What is the difference between IS-A and HAS-A relationships in LLD?
Fast recall
Quick revision
Core answer
Understanding of fundamental OOP relationships and knowing when to use inheritance versus composition in class design.
Overusing inheritance for code reuse even when there's no logical IS-A relationship, leading to fragile designs.
Mention the 'favor composition over inheritance' principle and give a concrete example where you'd choose composition to avoid tight coupling.
Interview-ready guide
Interview answer
IS-A and HAS-A are the two fundamental ways classes relate, and choosing between them is one of the most important calls in low-level design. IS-A is inheritance: a subclass is a more specialized type of its parent, so a Car is-a Vehicle, and I express that with extends. HAS-A is composition or aggregation: one class holds a reference to another as a field, so a ParkingLot has-a list of ParkingFloors. Composition usually implies ownership and a shared lifecycle, while aggregation is a looser version where the part can outlive the whole.
The test I apply is to ask whether X is genuinely a kind of Y in every context. If a car really is a vehicle everywhere it's used, inheritance is fine. But if the relationship is really X is made of Y or X uses Y, that's composition. The classic example of getting it wrong is making a Stack extend ArrayList. A stack is not really an array list, and by inheriting you accidentally expose methods like add-at-index that let callers violate the stack's contract. The composition version holds a private list and exposes only push and pop, so the API stays clean.
That's why the standard guidance is to favour composition over inheritance. Composition gives looser coupling and lets you swap the inner part at runtime, whereas inheritance is locked in at compile time and tends to produce brittle, deep hierarchies. I only reach for inheritance when the is-a relationship is genuinely true and permanent.
What is the difference between IS-A and HAS-A relationships in LLD
Direct Answer
IS-A represents inheritance where a subclass IS-A specialized type of the parent class (Car IS-A Vehicle). HAS-A represents composition or aggregation where one class HAS-A reference to another (ParkingLot HAS-A list of ParkingFloors). Composition implies ownership and lifecycle dependency, while aggregation implies a weaker relationship. Favor composition over inheritance unless there's a true specialization relationship.
Deep Dive & Architecture
Subtype substitutability
Car extends Vehicle is valid when every operation promised by Vehicle also makes sense for Car. Callers should be able to use a Car wherever a Vehicle is expected without special checks or surprising exceptions. In Java, class inheritance and interface implementation both create an IS-A type relationship.
HAS-A means collaboration
ParkingLot has a collection of ParkingFloor objects, and CheckoutService has a PaymentGateway. The containing class delegates work to the object it holds. Composition often gives a clear ownership relationship; aggregation is looser when the referenced object can exist independently.
Composition is safer for behaviour reuse
A stack should hold a list rather than extend ArrayList, because a stack must expose only push, pop, and peek. Inheriting ArrayList would also expose insertion in the middle and break the stack contract. Use inheritance for a stable semantic hierarchy; use composition when one object merely uses another or when the behaviour may need to be swapped.
Common Mistakes & Pitfalls
Overusing inheritance for code reuse even when there's no logical IS-A relationship, leading to fragile designs.
Check your understanding
Answer these without looking back at the article.
- What is Low-Level Design (LLD) and why does it exist? It translates high-level architecture into concrete class structures, relationships, and APIs that a developer can directly implement, bridging the gap between architecture and code.
- What is Low-Level Design (LLD) and what does an LLD interview expect you to do?
- What is the step-by-step process for solving an LLD interview question?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.