Class Relationships
Class Relationships in Java: IS-A, HAS-A, Association, Aggregation, Composition
Classes in a program rarely work alone. Class relationships describe how one class is connected to another. They fall into one hierarchy:
In simple words:
IS-A = "is a type of". HAS-A = "holds or uses". Aggregation and Composition are stronger forms of HAS-A.
Quick Overview
| Term | Meaning | Strength |
|---|---|---|
| IS-A | Child is a type of parent | Very strong (fixed at compile time) |
| Association | Two classes are related and can use each other | Weakest |
| Aggregation | Association where the part can live without the whole | Medium |
| Composition | Association where the part dies with the whole | Strongest |
1Association < Aggregation < Composition2 (weakest) (strongest)1. IS-A Relationship (Inheritance)
A child class is a type of the parent class. It is achieved with extends (classes) or implements (interfaces).
1Animal2 ↑3 Dog → Dog IS-A Animalclass Animal {void eat() {System.out.println("Eating...");}}class Dog extends Animal {void bark() {System.out.println("Barking...");}}public class Main {public static void main(String[] args) {Dog dog = new Dog();dog.eat(); // inheriteddog.bark();}}// Output:// Eating...// Barking...
Test: Say "B is a A". If it sounds natural, use inheritance.
2. HAS-A Relationship
A class holds a reference to another class as a field. It is the parent idea behind Association, Aggregation, and Composition.
1Car HAS-A Engine2Employee HAS-A Addressclass Engine {void start() {System.out.println("Engine started");}}class Car {private Engine engine = new Engine(); // HAS-Avoid drive() {engine.start();System.out.println("Car is moving");}}
HAS-A is not a keyword. It is just an object stored as a field.
3. Association
Association is the most general relationship. Two classes know about or use each other, but neither one owns the other. Both can exist independently.
1Student ─────── Teacher2 (uses / knows)class Teacher {String name;Teacher(String name) {this.name = name;}}class Student {String name;Student(String name) {this.name = name;}void learnFrom(Teacher teacher) { // just uses it, no ownershipSystem.out.println(name + " is learning from " + teacher.name);}}public class Main {public static void main(String[] args) {Teacher t = new Teacher("Anita");Student s = new Student("Ravi");s.learnFrom(t);}}// Output:// Ravi is learning from Anita
Types of Association by Multiplicity
| Type | Meaning | Example |
|---|---|---|
| One-to-One | One object linked to one | Person ↔ Passport |
| One-to-Many | One object linked to many | Teacher → Students |
| Many-to-One | Many objects linked to one | Employees → Company |
| Many-to-Many | Many linked to many | Students ↔ Courses |
4. Aggregation (Weak HAS-A)
Aggregation is an association where one class holds another class's object, but the held object can exist independently of the class holding it. The held object is usually created outside and passed in.
Part = the object being held. Whole = the object that holds it.
1Department ◇────── Teacher2(whole) (part, lives independently)class Teacher {String name;Teacher(String name) {this.name = name;}}class Department {String title;Teacher teacher; // created outside, passed inDepartment(String title, Teacher teacher) {this.title = title;this.teacher = teacher;}}public class Main {public static void main(String[] args) {Teacher t = new Teacher("Anita");Department d = new Department("Computer Science", t);d = null; // Department is gone...System.out.println(t.name); // ...but Teacher still exists: Anita}}
5. Composition (Strong HAS-A)
Composition is the strongest form of HAS-A. The whole owns the part, and the part cannot exist without the whole. The part is usually created inside the owner.
1House ◆────── Room2(whole) (part, dies with the house)class Room {String name;Room(String name) {this.name = name;}}class House {private Room bedroom;private Room kitchen;House() {bedroom = new Room("Bedroom"); // created and owned insidekitchen = new Room("Kitchen");}void showRooms() {System.out.println(bedroom.name + ", " + kitchen.name);}}public class Main {public static void main(String[] args) {House house = new House();house.showRooms();// When the House is gone, its Rooms go with it}}// Output:// Bedroom, Kitchen
Aggregation vs Composition
| Feature | Aggregation | Composition |
|---|---|---|
| Ownership | Weak | Strong |
| Lifecycle | Part lives independently | Part dies with the whole |
| Object created | Outside, then passed in | Inside the owner |
| UML symbol | Empty diamond ◇ | Filled diamond ◆ |
| Example | Department and Teacher | House and Room |
All Relationships Together: University Example
import java.util.ArrayList;import java.util.List;// IS-A: Teacher and Student are types of Personclass Person {String name;Person(String name) {this.name = name;}}class Teacher extends Person {Teacher(String name) {super(name);}}class Student extends Person {Student(String name) {super(name);}// Association: Student uses Teacher, no ownershipvoid learnFrom(Teacher teacher) {System.out.println(name + " is learning from " + teacher.name);}}// Aggregation: Department holds Teachers created outsideclass Department {private String title;private List<Teacher> teachers = new ArrayList<>();Department(String title) {this.title = title;}String getTitle() {return title;}void addTeacher(Teacher teacher) {teachers.add(teacher);}void show() {System.out.println(" Department: " + title);for (Teacher t : teachers) {System.out.println(" Teacher: " + t.name);}}}// Composition: University creates and owns its Departmentsclass University {private String name;private List<Department> departments = new ArrayList<>();University(String name) {this.name = name;departments.add(new Department("Computer Science")); // created insidedepartments.add(new Department("Mathematics"));}Department getDepartment(String title) {for (Department d : departments) {if (d.getTitle().equals(title)) {return d;}}return null;}void showInfo() {System.out.println("University: " + name);for (Department d : departments) {d.show();}}}public class Main {public static void main(String[] args) {Teacher anita = new Teacher("Anita"); // exists independentlyStudent ravi = new Student("Ravi");University uni = new University("Global Tech");uni.getDepartment("Computer Science").addTeacher(anita);ravi.learnFrom(anita);uni.showInfo();}}// Output:// Ravi is learning from Anita// University: Global Tech// Department: Computer Science// Teacher: Anita// Department: Mathematics
| Relationship | Where it appears in the example |
|---|---|
| IS-A | Teacher and Student extend Person |
| Association | Student uses Teacher in learnFrom() |
| Aggregation | Department holds Teacher objects created outside |
| Composition | University creates and owns its Departments |
How to Decide
1Is B a type of A? → IS-A (Inheritance)2Does A just use or know B? → Association3Does A hold B, but B can exist alone? → Aggregation4Does A own B, and B dies with A? → CompositionIS-A vs HAS-A
| Feature | IS-A | HAS-A |
|---|---|---|
| Meaning | Child is a type of parent | Class contains another object |
| Achieved with | extends / implements | Object as a field |
| Coupling | Tight | Loose |
| Flexibility | Fixed at compile time | Can change at runtime |
| Example | Dog IS-A Animal | Car HAS-A Engine |
Design Tip
"Favor composition over inheritance." Use inheritance only for a true IS-A relationship. For code reuse without IS-A, hold an object and delegate to it.
Interview Definitions
| Term | Definition |
|---|---|
| IS-A | A relationship where a child class is a specialized type of a parent class, achieved by inheritance. |
| HAS-A | A relationship where a class contains a reference to another class as a field. |
| Association | A general relationship in which two classes are connected and can use each other. |
| Aggregation | A weak HAS-A association where the contained object can exist independently of the container. |
| Composition | A strong HAS-A association where the contained object cannot exist without the container. |
Remember
IS-A = Inheritance. HAS-A = Association. Aggregation = HAS-A without ownership of lifecycle. Composition = HAS-A with full ownership.