SysDesignAtlas

OOP Concepts, Quickly

A fast refresher of the object-oriented basics every LLD round assumes — classes, encapsulation, abstraction, inheritance, polymorphism — with one small running example and the sentence to say for each.

LLDEasy8 min readooplldfundamentalsbasics
Confidence

Later lessons use words like "encapsulate", "override" and "abstract" without stopping to explain them. This page is the 15-minute version of those words, so the rest reads smoothly. If you already know them cold, skim the table and the check-yourself questions, then move on to Objects, in Practice.

#Classes and objects

A class is a blueprint. An object is one thing built from it. A class has two parts:

  • State — the data each object holds (its fields).
  • Behaviour — what it can do (its methods).

A constructor runs when the object is created and sets its starting state. Make it refuse to build an invalid object, and the rest of your code never has to wonder.

java
class Vehicle {
    private final String plate;     // state
    private final int wheels;

    Vehicle(String plate, int wheels) {          // constructor
        if (plate == null || plate.isBlank())
            throw new IllegalArgumentException("plate required");
        this.plate = plate;
        this.wheels = wheels;
    }

    long fuelCost(int km) { return km * 6L * 100; }   // behaviour: paise
}

Vehicle a = new Vehicle("KA01AB1234", 4);    // two objects,
Vehicle b = new Vehicle("MH12XY9876", 2);    // one class

#Access modifiers

They decide who can touch a member. Default to the narrowest one that works.

ModifierVisible to
privateOnly the same class
(none)Same package
protectedSame package, plus subclasses
publicEveryone

#Static versus instance

An instance member belongs to one object (plate). A static member belongs to the class itself — one copy, shared by all objects, usable without creating any (Math.max, a counter, a constant).

Use static for things that are genuinely not about one object: constants, pure helper functions, factory methods. Mutable static state is a shared global in disguise, and in a multi-threaded design it is a bug waiting to happen.

#The four pillars

  1. 1 Encapsulation "Data is private; callers use actions that enforce the rules."
  2. 2 Abstraction "Callers see what it does, not how it does it."
  3. 3 Inheritance "A child reuses and extends a parent, but only for a true is-a."
  4. 4 Polymorphism "Same call, different behaviour depending on the real object."

Encapsulation and polymorphism carry most of the weight in an LLD round. Inheritance is the one to use sparingly.

The four pillars, each with the one sentence worth saying aloud.

#Encapsulation: hide the data, expose the actions

Keep fields private and let callers change them only through methods that check the rules. Wallet.pay(amount) can refuse to go below zero; a public balance field cannot.

#Abstraction: show the what, hide the how

The caller of payment.charge(amount) should not know whether it talks to UPI, a card network or a wallet. Abstraction is the habit of exposing a small, stable surface and keeping the messy details behind it. Encapsulation protects the data; abstraction simplifies the view.

#Inheritance: reuse through is-a

java
class Vehicle {
    protected final String plate;
    Vehicle(String plate) { this.plate = plate; }
    long fuelCost(int km) { return km * 6L * 100; }
}

class ElectricCar extends Vehicle {        // ElectricCar IS-A Vehicle
    ElectricCar(String plate) { super(plate); }

    @Override
    long fuelCost(int km) { return km * 1L * 100; }   // cheaper per km
}

The child gets everything the parent has, and may replace some of it. Powerful, but it ties the child tightly to the parent: change the parent and every child can break. That is why the next lesson argues for holding objects instead.

#Polymorphism: one call, many behaviours

java
List<Vehicle> fleet = List.of(new Vehicle("A1"), new ElectricCar("B2"));
long total = 0;
for (Vehicle v : fleet)
    total += v.fuelCost(100);      // each object runs ITS OWN version

The loop never asks "are you electric?". Java looks at the real object at the moment of the call and runs that class's method. This is called runtime dispatch (dynamic dispatch), and it is what lets you add a new type without touching the loop.

#Abstract class versus interface

Both let you write code against a general type and plug in different implementations. They differ in what they carry.

Abstract classInterface
PurposeA partly-built parent for a closely related familyA contract any class can sign up to
Can hold fields (state)YesNo (only constants)
ConstructorYesNo
InheritingA class extends only oneA class can implement many
Typical useShared code plus a few blanks to fill in"Anything that can charge money", "anything that can be notified"
java
interface PaymentMethod {                  // the contract
    boolean charge(long paise);
}

abstract class BasePayment implements PaymentMethod {   // shared plumbing
    protected void log(long paise) { System.out.println("charging " + paise); }
}

class UpiPayment extends BasePayment {
    public boolean charge(long paise) { log(paise); return true; }
}
class WalletPayment extends BasePayment {
    private long balance = 50_000;          // ₹500
    public boolean charge(long paise) {
        if (paise > balance) return false;
        log(paise);
        balance -= paise;
        return true;
    }
}

Rule of thumb: start with an interface for what callers need. Add an abstract class only if several implementations really share code or state.

#Composition, as a preview

Inheritance says "is a". Composition says "has a": a class holds another object in a field and calls it.

java
class Order {
    private final PaymentMethod payment;       // has-a, swappable
    Order(PaymentMethod payment) { this.payment = payment; }

    boolean checkout(long paise) { return payment.charge(paise); }
}

Order is not a kind of payment, so it holds one. You can hand it UpiPayment or WalletPayment and never edit Order. When in doubt between the two, hold; inherit only for a true is-a. The full argument is in Objects, in Practice and SOLID.

#Check yourself

Class versus object?Show answer
A class is the blueprint; an object is one instance built from it. A class defines state (fields) and behaviour (methods); a constructor sets the starting state.
What is encapsulation, in one interview sentence?Show answer
Data is private, and callers go through actions like pay() or withdraw() that enforce the rules in one place.
Encapsulation versus abstraction?Show answer
Encapsulation protects data by hiding it behind methods. Abstraction simplifies the view, showing what something does and hiding how.
Overriding versus overloading?Show answer
Overriding replaces a parent method with the same signature in a subclass and is chosen at runtime by the real object. Overloading is same name with different parameters in one class and is chosen at compile time.
What is runtime dispatch?Show answer
When you call a method through a parent-typed reference, Java runs the version belonging to the real object at that moment. It lets you add new types without editing the calling code.
Abstract class versus interface?Show answer
An abstract class is a partly-built parent that can hold state and a constructor, and a class extends only one. An interface is a pure contract a class can implement many of. Start with an interface; add a base class only for shared code.
Static versus instance?Show answer
Instance members belong to one object. Static members belong to the class: one shared copy, with no object needed. Mutable static state is a hidden global and unsafe across threads.
Inheritance versus composition in one line?Show answer
Inheritance is "is a" and ties child to parent; composition is "has a" and lets you swap the held object. Inherit only for a true is-a, otherwise hold a reference.

#What to carry forward

  • A class is the blueprint, an object is one instance; constructors should refuse invalid state.
  • Encapsulation: private data, rule-enforcing actions. Abstraction: callers see the what, not the how.
  • Polymorphism means overriding plus runtime dispatch: one call, behaviour chosen by the real object.
  • Interface for the contract, abstract class only for shared code; prefer composition to inheritance.
  • Keep access narrow and avoid mutable static state.
00:00
/ 8m