Skip to content
LLD Learn/OOP Foundations & UML
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

The Four Pillars, Revisited for Interviews

4 min read

You'll learn to

  • -Explain encapsulation, abstraction, inheritance and polymorphism in plain words, each with a small example
  • -Spot a class that has private fields but still fails to protect itself
  • -Say why polymorphism, not code reuse, is the real point of inheritance

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

Every computer-science student can recite the four pillars of object-oriented programming. What separates a strong LLD candidate is using them with judgment, because it is entirely possible to write code that technically has all four and is still a bad design. First, the four ideas in one plain sentence each.

  • -Encapsulation: keep an object's data together with the code that is allowed to change it, and stop everyone else from changing it any other way.
  • -Abstraction: show callers what an object does and hide how it does it.
  • -Inheritance: a new class starts from an existing class and adds or changes some behavior.
  • -Polymorphism: one call can behave differently depending on which kind of object receives it.

An Analogy: The ATM

An ATM is a good picture of the first two ideas. You cannot reach into the cash tray and take notes out. The machine only lets money leave through the withdraw button, after it has checked your balance. That is encapsulation. You also never see how it counts notes or updates your account. You press "withdraw 50" and it happens. That is abstraction.

Encapsulation Is About Invariants, Not Just Private Fields

An invariant is a rule that must always be true about an object, such as "an account's balance is never negative". Putting private on every field and generating a getter and a setter for each is not encapsulation. It is a longer way to expose the same mutable state. Real encapsulation means the class protects its own invariants. The demo below compares an account anyone can write to with one that only changes through a method that enforces the rule.

Press Run: the open account ends at -50, the protected one stays at 100

Abstraction: Hide the Complexity Behind the Right Contract

Abstraction means callers depend on what a class does, not how it does it. A PaymentProcessor.charge(amount) method hides card networks, retries and currency conversion, and the caller needs none of that. The interview point is that a good abstraction is shaped by what the caller needs today. Adding extra parameters and hooks nobody asked for is a design smell of its own.

Inheritance and Polymorphism: Substitutability Is the Point

Inheritance is not mainly a way to save typing. That is a side effect. Its real purpose is to make polymorphism possible: code written against a base type works with any of its subtypes without knowing which one it got. Suppose a checkout must accept a credit card or a wallet. The win is that checkout calls one method and never asks which kind of payment it holds.

One call, two behaviors: the checkout code never checks the type

A sanity check for any inheritance relationship: can you truthfully say "a [Subclass] is a [Superclass]" in every place the base class is used? "A DebitCard is a Payment" holds. "A Square is a Rectangle" does not hold the way most people assume. The Liskov Substitution chapter explains why.

Interview Signal

Why does a class with a public getter and setter for every private field not count as good encapsulation?

Weak Answer

"It does count - all the fields are private, so the data is protected."

Strong Answer

"Making fields private only to expose an unrestricted setter for each one doesn't protect anything - external code can still put the object into any state, including invalid ones. Real encapsulation means the class's methods enforce its invariants, so it's impossible to construct an invalid instance through the public interface."

Check Yourself1 / 5

What makes encapsulation more than just "making fields private"?

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!