OOP Fundamentals — Design Review

Easy
FundamentalsSOLIDOOP

OOP Fundamentals for LLD Interviews

Before tackling design problems, master these foundational concepts:

The 4 Pillars of OOP

1. Encapsulation Hide internal state. Only expose what's necessary through a public interface.

class BankAccount {
    private double balance; // private — hidden
    public void deposit(double amount) { // controlled access
        if (amount > 0) balance += amount;
    }
}

2. Abstraction Hide complexity behind simple interfaces.

interface Shape {
    double area(); // I don't care HOW, just that it works
}

3. Inheritance IS-A relationship. Reuse and extend.

class Car extends Vehicle { ... } // Car IS-A Vehicle

4. Polymorphism Same interface, different behavior.

Shape s = new Circle(5);
s.area(); // calls Circle.area(), not Rectangle.area()

SOLID Principles

PrincipleMeaningViolation Example
S — Single ResponsibilityOne class, one reason to changeUserManager handles auth, DB, and email
O — Open/ClosedOpen for extension, closed for modificationAdding a new payment method requires editing existing class
L — Liskov SubstitutionSubclass must be usable in place of parentSquare extends Rectangle breaks width/height independence
I — Interface SegregationDon't force clients to implement unused methodsAnimal interface has fly() but Fish implements it
D — Dependency InversionDepend on abstractions, not concretionsOrderService directly instantiates MySQLDatabase

Common Design Patterns in LLD Interviews

PatternTypeUse When
SingletonCreationalOnly one instance needed (ParkingLot, ATM)
FactoryCreationalCreate objects without specifying exact class
ObserverBehavioralOne-to-many event notifications
StrategyBehavioralSwap algorithms at runtime
StateBehavioralObject behavior changes with internal state
CompositeStructuralTree structures (File System)
DecoratorStructuralAdd behavior without modifying class

Practice Exercise

Implement the following to test your understanding:

!Constraints

- Implement a Shape hierarchy with Circle, Rectangle, Triangle - Each shape must implement area() and perimeter() - Create a ShapeFactory that creates shapes by type string - Demonstrate polymorphism by storing different shapes in a List<Shape> - Ensure all shapes follow the Liskov Substitution Principle
Output

Run or submit your code to see output here.