Lecture 07 Object Relationship
Lecture 07 Object Relationship
Lecture 07
1
“is-a” vs. “has-a”
– “is-a”
• Inheritance
• subclass object treated as superclass object
• Example: Car is a vehicle
– Vehicle properties/behaviors also car properties/behaviors
– “has-a”
• Composition
• Object contains one or more objects of other classes as
members
• Example: Car has a steering wheel
2
Association
12
HAS-A relationship
package relationships;
class Car {
// Methods implementation and class/Instance members
private String color;
private int maxSpeed;
public void carInfo(){
System.out.println("Car Color= "+color + " Max Speed= " + maxSpeed);
}
public void setColor(String color) {
this.color = color;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
13
HAS-A relationship
class Honda extends Car{
//
Honda extends Car and thus inherits all methods from Car (except final and
static)
//Honda can also define all its specific functionality