Inheritance vs Composition vs Aggregation

   Inheritance  Composition Aggregation
 Relationship type “is a” relationship “part of” relationship “has a” relationship
Lifetime of part NA The lifetime of part is managed by the whole The lifetime of part is not managed by the whole
 Example Tiger is a Animal Engine is part of the Car.When Car is destroyed, Engine is destroyed along with it. Employee has a Address. Address still continues to exist even if the Employee ceases to exist.

Java Code

Inheritance

package com.test.inheritance;

public class Animal {

public void animalType(){

System.out.println(“This is an Animal”);

}

public static void main(String[] args) {

Animal animal = new Tiger();

animal.animalType();

}

}

class Tiger extends Animal{

public void animalType(){

System.out.println(“This is a Tiger”);

}

}

Composition

package com.test.composition;

public class Car {

private Engine engine;

public Car() {

engine = new Engine();

}

public static void main(String[] args) {

Car car= new Car();

}

}

class Engine{

}

// Here if we destroy the Car object then the Engine object will also get destroyed.

Aggregation

package com.test.aggregation;

public class Employee {

public Address getAddress() {

return address;

}

public void setAddress(Address address) {

this.address = address;

}

private Address address;

public Employee(Address address) {

this.address = address;

}

public static void main(String[] args) {

Address address = new Address(“New Delhi”);

Employee emp = new Employee(address);

System.out.println(emp.getAddress().getAddressDetails());

emp = null;

System.out.println(address.getAddressDetails());

}

}

class Address {

private String addressDetails;

public Address(String addressDetails) {

this.addressDetails = addressDetails;

}

public String getAddressDetails() {

return addressDetails;

}

public void setAddressDetails(String addressDetails) {

this.addressDetails = addressDetails;

}

}

Output
New Delhi
New Delhi

// Here the Address Object is not destroyed on destroying the Employee Object

7 comments

  1. Can I conclude like this:

    Association:
    Two objects are related to each other but can exist as an individual without each other. Ex. manager & employee.

    Aggregation: has-a
    Two objects (A & B) are related to each other but in a unidirectional way i.e., A can exist without B but B has to have A. Ex. employee & address i.e., address exists whether or not the employee is there but employee has to have an address.

    Composition:
    Two objects are related to each other in such a way that both require each other strongly. Ex. employees can’t exist without an organization and an organization also can’t exist without employees.

    1. Let us understand this in terms of whole and part(A whole is composed of parts)
      We can view aggregation as a loose coupling between whole and part where as composition is kind of tight coupling between whole and part.

      In Composition, the parts does not have any significance without the whole. Whereas in aggregation the part is independent of the whole but the whole is incomplete without the part.

      1. In Composition, the parts does not have any significance without the whole. That thing I understand now, but what about the whole? Is the whole also dependent on the parts?

      2. The whole is always dependent upon the parts for both composition and aggregation. The total dependence of part on the whole differentiates composition from aggregation.

Leave a Reply

Your email address will not be published. Required fields are marked *