Association Types in Java: Inheritance, Aggregation and Composition
Java models how classes relate to one another through three core association styles: inheritance (an is-a link), aggregation (a has-a link) and composition (a part-of link). Each one shapes how tightly objects are coupled and how they behave when one object is removed. Below we walk through all three with plain explanations and working code samples.
1. Inheritance in Java (Is-A Relationship)
Inheritance is a foundational feature of object-oriented programming. Through it, one class can take on the fields and methods of another class, so the child class reuses and extends the parent's behaviour. Because the child is a specialised version of the parent, this link is called the Is-A relationship.
For a deeper walkthrough of the topic, see the full guide on inheritance in Java.
2. Aggregation in Java (Has-A Relationship)
Aggregation describes a Has-A link. It is unidirectional, which is why it is also called a one-way association. A bank can have employees, but the reverse does not hold, and that is what makes the relationship one-directional. In Java aggregation, the objects of the two classes do not affect each other's lifecycle.
Aggregation Example: Bank and Customer
Consider a bank and a customer. A customer belongs to a bank, yet if a customer stops visiting, the bank keeps running because it can still operate. This demonstrates the Has-A link: a bank HAS-A customer.
class Bank {
String nameOfBank;
int IFSC;
Bank(String nameOfBank, int IFSC) {
this.nameOfBank = nameOfBank;
this.IFSC = IFSC;
}
public void displayAllDetails(Customer customer) {
System.out.println("Name of Bank = " + nameOfBank);
System.out.println("IFSC of Bank = " + IFSC);
System.out.println("Name of Customer = " + customer.nameOfCustomer);
System.out.println("Account number of Customer = " + customer.accountNumber);
System.out.println("Amount of Customer = " + customer.amount);
}
}
class Customer {
String nameOfCustomer;
int accountNumber;
int amount;
Customer(String nameOfCustomer, int accountNumber, int amount) {
this.nameOfCustomer = nameOfCustomer;
this.accountNumber = accountNumber;
this.amount = amount;
}
}
class Branch {
public static void main(String arg[]) {
Bank bank = new Bank("AXIS", 12345);
Customer customer = new Customer("Ram", 123456789, 100000);
bank.displayAllDetails(customer);
}
}Output:
Name of Bank = AXIS
IFSC of Bank = 12345
Name of Customer = Ram
Account number of Customer = 123456789
Amount of Customer = 100000Here we have two classes, Bank and Customer. Only a portion of the customer's data is needed, so a Customer object is created and passed as an argument, after which its details are used inside the Bank method.
3. Composition in Java (Part-Of Relationship)
Composition is a stricter form of aggregation. The two entities are highly dependent on each other, and one cannot exist without the other, which is why it is described as a part-of relationship. It is a tightly restricted form of association.
Composition Example: Car and Engine
A car has an engine. Composition builds a strong bond between objects: if the owning object is destroyed, its members are destroyed along with it. For instance, when the car is destroyed, the engine goes with it.
class Car {
// final ensures the engine is initialized
private final Engine engine;
String nameOfCar;
String model;
public Car(String nameOfCar, String model) {
engine = new Engine("POWERHIGH", "ABC");
this.nameOfCar = nameOfCar;
this.model = model;
}
public void showAlldetails() {
System.out.println("Name of Car =" + nameOfCar);
System.out.println("Name of Model =" + model);
System.out.println("Engine used in Car =" + engine.typeOfEngine);
System.out.println("Engine name of Car =" + engine.nameOfEngine);
}
}
class Engine {
String typeOfEngine;
String nameOfEngine;
Engine(String typeOfEngine, String nameOfEngine) {
this.typeOfEngine = typeOfEngine;
this.nameOfEngine = nameOfEngine;
}
}
class ShowRoom {
public static void main(String arg[]) {
Car car = new Car("BMW", "5X");
car.showAlldetails();
}
}Output:
Name of Car =BMW
Name of Model =5X
Engine used in Car =POWERHIGH
Engine name of Car =ABCIn this example there are two classes, Car and Engine. The Engine class depends entirely on Car because an engine is part of a car, and without an engine the car is nothing. So inside the Car class an Engine object is created and initialized, making the two classes highly dependent.
Quick Comparison
- Inheritance (Is-A): a child class acquires the fields and methods of a parent class.
- Aggregation (Has-A): a unidirectional link where objects keep independent lifecycles.
- Composition (Part-Of): a strong, dependent link where the part cannot outlive the whole.
JavaGoal Leave a Comment
