0% found this document useful (0 votes)
3 views9 pages

Java Developers 1755091824

Uploaded by

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

Java Developers 1755091824

Uploaded by

kzftb6wb7g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sanjay Kumar

IS-A vs HAS-A
Relationship in

Java
If you mess this up, your OOP
code falls apart.
IS-A Relationship

→ Defined by inheritance
→ Think: Dog IS-A Animal
→ Creates a parent-child
hierarchy
→ Use extends and add extra
features in child
HAS-A Relationship

→ Defined by composition Strong


Composition (Aggregation)
→ Think: Car HAS-A Engine
→ one class contains a reference to another
class
→ Promotes code reusability and loose
coupling
→ composition focuses on object
collaboration
→ Makes systems easier to maintain and test
Why it matters:

→ Wrong use of IS-A = fragile, rigid code


→ HAS-A gives flexibility and better
design
→ Strong Composition (Aggregation)
→ Weak Composition (Association)
→ Great interviewers ask this to test
OOP real understanding
Java inheritance example where Car and
Truck inherit from a common superclass
Vehicle: // Superclass
class Vehicle {
void start() {
System.out.println("Vehicle is starting...");
}
}

// Subclass: Car
class Car extends Vehicle {
void openTrunk() {
System.out.println("Car trunk is open.");
}
}

// Subclass: Truck
class Truck extends Vehicle {
void loadCargo() {
System.out.println("Truck is loading cargo.");
}
}

// Main class to test


public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start(); // inherited method
car.openTrunk(); // subclass-specific method

Truck truck = new Truck();


truck.start(); // inherited method
truck.loadCargo(); // subclass-specific method
}
}
Strong Composition (Composition Proper)

→ Lifespan is tied to the container object.


→ If the outer object is destroyed, so are the
inner objects.
class Engine {
Example: void start() {
System.out.println("Engine started.");
}
}

class Car {
private Engine engine;

public Car() {
this.engine = new Engine(); // Strong
composition
}

public void startCar() {


engine.start();
System.out.println("Car is ready to go.");
}
}

Note : Car owns the Engine. If the Car is destroyed,


the Engine will be destroyed too.
Weak Composition (Aggregation)
→ Object exists independently of the container class.
→ Allows object sharing and looser coupling.
class Department {
Example: String name;

public Department(String name) {


this.name = name;
}
}

class Employee {
private String name;
private Department dept; // Aggregation

public Employee(String name, Department dept)


{
this.name = name;
this.dept = dept;
}

public void showDetails() {


System.out.println(name + " works in " +
dept.name + " department.");
}
}

Note : Department and Employee have a loose


relationship.
The department can exist without the employee and
vice versa.
Don’t just use inheritance by
default.

Design with intent, if possible


follow design pattern.
Mastering IS-A vs HAS-A is how
you write clean, scalable code.

Comment "OOP Mastery" if


you're done writing spaghetti.
Follow
SANJAY KUMAR

Co-Founder, Cyberinfomines
Technology Pvt Ltd

Message me directly
or visit
Cyberinfomines
Technology to get
started.

You might also like