Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
1-5-Reusing

1-5-Reusing

Ratings: (0)|Views: 0 |Likes:
Published by Arsalan Ahmed
Reusing
Reusing

More info:

Published by: Arsalan Ahmed on Dec 26, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/26/2012

pdf

text

original

 
ReusingClasses
CompositionInheritanceInheritance and Object CreationProtected accessFinal Components
OOAD 1998/99Claudia
Niederée, Joachim W. SchmidtSoftware Systems Institute
c.niederee@tu-harburg.de http://www.sts.tu-harburg.de
OOAD98/99-STS-Reusing Classes2
Reusing Classes
When you need a class, you can:1) Get the perfect one off the shelf (one extreme)2) Write it completely from scratch (the other extreme)3) Reuse an existing class with composition4) Reuse an existing class or class framework withinheritance
 
OOAD98/99-STS-Reusing Classes3
Composition vs. Inheritance
CarWheel[]Window[]Door[]Engine
Composition:
“Has-A/ “Part-OfrelationshipCode reuse
Shapedraw()erase()Circledraw()erase()Sqaredraw()erase()Linedraw()erase()
Inheritance:“Is-A” relationshipinterface duplicationfor interchangeableobjectsCode Reuse
OOAD98/99-STS-Reusing Classes4
Composition Syntax
MyNewClassInstance ofexisting classInstance ofexisting classInstance ofexisting classInstance ofexisting classEtc.
class
Car {Engine eng =
new
Engine(100);Wheel[] wheels =
new
Wheel[4];Horn horn =
new
Horn();...}
Instances of existing classes asdata members“Has- A” relationshipFlexibility: Can change objectsat run time!Component Creation: Inconstructor or where defined
 
OOAD98/99-STS-Reusing Classes5
class
Mixer { 
 private
String brand;Mixer(String brand) { 
this
.brand = brand;System. out. println("Mixer constructed");} 
 public
String toString() {
return
 
brand + “ Mixer“;}}
class
Toaster {Toaster() {System. out. println(“Toaster constructed");} 
 public
String toString() {
return
“Toaster“; }}
class
Knife {String purpose;Knife(String purpose) {
this
.purpose = purpose;System. out. println(“Knife constructed");}
public
String toString() {
return
“Knife for “ +purpose; }}
OOAD98/99-STS-Reusing Classes6
 
 public class
Kitchen {Mixer =
new
Mixer(“Braun");Toaster toaster;Knife knife;int length, width;Kitchen(
int
length,
int
width) { 
this
.toaster =
new
Toaster();this.length = 352; this.width = 234;}
void 
print() { 
if
( knife == null)knife =
new
 
Knife(“Vegetable");System. out. println(" knife " + knife);System. out. println(" mixer " + mixer);System. out. println(" length = " + length);}
 public static void 
main( String args[]) {Kitchen k =
new
Kitchen();k. print();}}
DelayedinitializingInitializingat pointof definitionInitializingin constructor

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->