Transcript Inheritance

Composition - 1
J. Sant.
Agenda




Define Composition
Reuse and Composition.
Composition in Java.
Composition Exercise with simple
containment.
Composition

A programmer can specify that a new class is
composed of instances of previously defined
classes.

A Car class may be composed of instances of an
Engine class, a Chassis class and a DriveTrain
class.
Object-Oriented Code Re-use

Composition is used to represent “has a”
relationship.
A car HAS AN engine.
A gas station HAS pumps.

Inheritance is used to represent “is a”
relationships.
A savings account IS A type of account.
A barrel type IS A type of container.
Composition – Why?


Composition is probably the easiest method
of improving code reuse. All that is
necessary is to define instance variables that
are objects then construct them in the
constructor.
Composition is generally preferred to
inheritance as a code reuse method (see
Bloch).
Composition Example
class Course {
private int id;
// attribute that is itself an object.
private TextBook textbook;
}
Gun.java
public class Gun {
public static final int
DEFAULT_CLIP_SIZE =7;
public static final int DEFAULT_RANGE =700;
private int clipSize = DEFAULT_CLIP_SIZE;
private int range = DEFAULT_RANGE;
private int currentRounds =
DEFAULT_CLIP_SIZE;
private String name;
public Gun(String name, int clipSize, int range ){
this.name = name;
this.clipSize = clipSize;
this.range = range;
this.currentRounds = clipSize;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public int getClipSize(){
return this.clipSize;
}
public int getRange(){
return range;
}
public int getCurrentRounds(){
return currentRounds;
}
public void fire( int noRounds ){
if( noRounds <= currentRounds ){
currentRounds -= noRounds;
} else {
currentRounds = 0;
}
}
public void reLoad(){
currentRounds = clipSize;
}
}
Player.java
public class Player {
private Gun armament;
private String name;
public Player( String name, Gun gun ){
this.name = name;
armament = gun;
}
public String getName(){
return this.name;
}
public void switchGun( Gun newGun ){
armament = newGun;
}
public void fire( int noRounds ){
armament.fire(noRounds);
}
public int roundsLeft(){
return armament.getCurrentRounds();
}
}
Player-Gun Discussion.


Why is Player a composite class.
Player uses a strategy called delegation
where is completes a task by delegating it to
one of the contained objects. Where does
this occur.
Your turn.


Define a simple class, Leg. This defines a leg
in a trip with a single stopover. Leg should
store only the name of the origin city, the
destination city and an integer value for the
length of the leg in kilometers.
Define a composite class TripWithStopover.
This stores an initial leg and a final leg. It
should be able to return the total distance of
the trip and also return the starting city and
the destination city.
Summary.



Composition allows a programmer to save
work by using pre-defined classes to
compose new classes.
In Java, you implement composition by
defining new classes using pre-defined
classes.
Delegation is when a containing (composite)
class completes a requested operation by
passing on the request to a contained object.
ance.