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.
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.
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.
| Modifier | Visible to |
|---|---|
private | Only the same class |
| (none) | Same package |
protected | Same package, plus subclasses |
public | Everyone |
#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 Encapsulation "Data is private; callers use actions that enforce the rules."
- 2 Abstraction "Callers see what it does, not how it does it."
- 3 Inheritance "A child reuses and extends a parent, but only for a true is-a."
- 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.
#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
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
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 versionThe 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 class | Interface | |
|---|---|---|
| Purpose | A partly-built parent for a closely related family | A contract any class can sign up to |
| Can hold fields (state) | Yes | No (only constants) |
| Constructor | Yes | No |
| Inheriting | A class extends only one | A class can implement many |
| Typical use | Shared code plus a few blanks to fill in | "Anything that can charge money", "anything that can be notified" |
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.
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
What is encapsulation, in one interview sentence?Show answer
Encapsulation versus abstraction?Show answer
Overriding versus overloading?Show answer
What is runtime dispatch?Show answer
Abstract class versus interface?Show answer
Static versus instance?Show answer
Inheritance versus composition in one line?Show answer
#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.