Skip to main content

Object Oriented Design: Relationship Types

Inheritance

Inheritance relationship defines a parent child relationship in which child is said to be type of parent. Child inherits common actions and attributes from parent. Inheritance relationship allows rich abstractions to reduce code repetition and to increase code reuse.
Consider the Passenger and StudentPassenger objects. StudentPassenger object is of type Passenger. StudentPassenger inherits all actions and attributes of Passenger. Additionally a StudentPassenger has a pass card and pays less than a normal Passenger. It is phrased as StudentPassenger is a Passenger.

Association

Association is a relationship in which one object uses another object to achieve some task. They don’t own each other and  their lifecycle is independent of each other.  
Consider the relationship between bus and bus station. They don’t own each other and they exists regardless of each other. It is phrased as Bus uses a Bus Stop to pick passengers.

Aggregation

Aggregation is like association, lifecycle of objects does not depend on each other. But unlike association, in aggregation relationship one object owns the other.
Consider the relationship between passenger and bus. A Bus and a passenger exist independent of each other but a passenger may belong to a bus. It is phrased as Bus has a passenger.

Composition

Composition relationship means that lifecycle of objects depend each other and one side of relationship is owned by other side.
Consider the relationship between a bus and engine. Lifecycle of bus and engine strongly depends on each other and engine belongs to bus. It is phrased as Bus owns an Engine.

Over All Picture

Diagram 1 depicts UML diagram of the related objects.


Below is the example code listing for defined relationships.

class Bus{
 Engine engine;
 List<Passenger> passengers;
 
 Bus(){
  // Bus owns an engine and engine's lifecycle depends on Bus object
  engine = new Engine();
 }
 
 void pickPassengers(BusStop busStop){}
}

class BusStop{
 List<Passenger> passengers;
 
 List<Passenger> getPassengers(){};
 void addPassenger(Passenger passenger){};
}

class Passenger{
 double payFee(){}
}
class StudentPassenger{
 Pass pass;
 double payFee(){}//overridden to pay less
}

class Pass{}
class Engine{}

class Main{
 
 public static void main(String[] args){
  
  Bus bus = new Bus();// not owned by BusStop, lifecycle does not depend on BusStop
  BusStop busStop = new BusStop();// not owned by Bus, lifecycle does not depend on Bus
  
  Passenger passenger1 = new Passenger();// lifecycle does not depend on BusStop or Bus
  Passenger passenger2 = new StudentPassenger(); // lifecycle does not depend on BusStop or Bus
  // StudentPassenger is also a passenger.
  
  busStop.addPassenger(passenger1); // busStop has a passenger
  busStop.addPassenger(passenger2); // busStop has a passenger
  
  bus.pickPassengers(busStop); // bus uses bus stop to pick passengers 
  // now Bus has passengers
  
 }
 
}

Comments