0% found this document useful (0 votes)
5 views16 pages

Lecture 07 Object Relationship

Uploaded by

Ahmed Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Lecture 07 Object Relationship

Uploaded by

Ahmed Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming

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

• The objects that are part of the association relationship


can be created and destroyed independently.
• Association is a semantically weak relationship (a
semantic dependency) between otherwise unrelated
objects.
• An association is a “using” relationship between two or
more objects in which the objects have their own
lifetime and there is no owner.
• Composition and Aggregation are the two forms of
association.
Association
Aggregation

• Aggregation is a specialized form of association between two or


more objects in which each object has its own life cycle but there
exists an ownership as well.
• An essential property of an aggregation relationship is that the
whole or parent (i.e. the owner) can exist without the part or child
and vice versa.
• The life cycles of parent objects and child objects are independent.
In a composition relationship, the death of a parent object also
means the death of its children.
• examplA department can have students but vice versa is not
possible and thus unidirectional in nature.
• In Aggregation, both the entries can survive individually which
means ending one entity will not affect the other entity.
composition
• If the parent object is destroyed, then the child objects also cease to
exist.
• The life cycle of the part or child is controlled by the parent that
owns it.
• Control can either be direct or transitive. That is, the parent may be
directly responsible for the creation or destruction of the child or the
parent may use a child that has been already created. Similarly, a
parent object might delegate the control to some other parent to
destroy the child object.
• As an example, a house may be composed of one or more rooms. If
the house is destroyed, then all of the rooms that are part of the
house are also destroyed. The following code snippet illustrates a
composition relationship between two classes, House and Room.
IS-A Relationship
• IS-A is a way of saying : This object is a type of that
object. Let us see how the extends keyword is used to
achieve inheritance.

public class Animal{


}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
} 8
IS-A Relationship
• Now based on the above example, In Object Oriented
terms following are true:
– Animal is the superclass of Mammal class.
– Animal is the superclass of Reptile class.
– Mammal and Reptile are sub classes of Animal class.
– Dog is the subclass of both Mammal and Animal classes.
• Now if we consider the IS-A relationship we can say:
– Mammal IS-A Animal
– Reptile IS-A Animal
– Dog IS-A Mammal
– Hence : Dog IS-A Animal as well
9
IS-A Relationship
• We can assure that Mammal is actually an Animal with the use of the
instance operator

public class Dog extends Mammal{


public static void main(String args[]){
Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
This would produce following result:
true
True 10
HAS-A relationship
• These relationships are mainly based on the usage.
This determines whether a certain class HAS-A certain
thing. This relationship helps to reduce duplication of
code as well as bugs.

public class Vehicle{


}
public class Speed{
}
public class Van extends Vehicle{
private Speed sp;
} 11
HAS-A relationship
• This shows that class Van HAS-A Speed.
• By having a separate class for Speed we do not
have to put the entire code that belongs to
speed inside the Van class.
• which makes it possible to reuse the Speed
class in multiple applications.

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

public void HondaStartDemo(){


Engine HondaEngine = new Engine();
HondaEngine.start();
}
}
14
HAS-A relationship
package relationships;
public class Engine {
public void start(){
System.out.println("Engine Started:");
}
public void stop(){
System.out.println("Engine Stopped:");
}
}
15
HAS-A relationship
package relationships;
public class RelationsDemo {
public static void main(String[] args) {
Honda myHonda = new Honda();
myHonda.setColor("RED");
myHonda.setMaxSpeed(180);
myHonda.carInfo();
myHonda.HondaStartDemo();
}
}
16

You might also like