What are the Four Pillars of OOP?
Learn the four pillars of OOP — encapsulation, abstraction, inheritance and polymorphism — with clear definitions, a Java example and interview questions.
Expected Interview Answer
The four pillars of object-oriented programming are Encapsulation, Abstraction, Inheritance, and Polymorphism — the principles that structure code around objects.
Encapsulation bundles data with the methods that operate on it and restricts direct access. Abstraction exposes only essential behavior while hiding complexity. Inheritance lets a class derive fields and behavior from a parent class to reuse and specialize code. Polymorphism lets objects of different classes respond to the same call in their own way. Together they enable modular, reusable, and extensible designs.
- Modular code organized around objects
- Reuse through inheritance and shared interfaces
- Extensibility without rewriting existing code
- Protected internal state via encapsulation
AI Mentor Explanation
Think of a cricket team. Encapsulation is each player guarding their own technique — you interact through their role, not their private practice. Abstraction is the scoreboard showing runs and wickets while hiding the mechanics. Inheritance is an all-rounder inheriting both batting and bowling skills from those specialisms. Polymorphism is the captain shouting "field" and each player taking their own position. Four principles, one well-run team.
Step-by-Step Explanation
Step 1
Encapsulation
Bundle data with methods; restrict direct access via access modifiers.
Step 2
Abstraction
Expose essential behavior through interfaces; hide implementation detail.
Step 3
Inheritance
Derive a class from a parent to reuse and specialise its members.
Step 4
Polymorphism
Let different classes respond to the same call in their own way.
What Interviewer Expects
- All four named with a one-line definition each
- A concrete example tying them together
- Distinction between abstraction and encapsulation
- How inheritance and polymorphism relate
Common Mistakes
- Listing only three pillars
- Confusing abstraction with encapsulation
- Describing inheritance as the same as polymorphism
- Giving definitions with no example
Best Answer (HR Friendly)
“The four pillars of OOP are encapsulation (protecting data), abstraction (hiding complexity), inheritance (reusing code from a parent class), and polymorphism (one interface, many behaviors). Together they make software modular, reusable, and easy to extend.”
Code Example
abstract class Account { // Abstraction
private double balance; // Encapsulation
public void deposit(double a) { balance += a; }
public abstract double interest();
}
class Savings extends Account { // Inheritance
public double interest() { return 0.04; } // Polymorphism
}
class Current extends Account {
public double interest() { return 0.0; } // Polymorphism
}Follow-up Questions
- What is the difference between abstraction and encapsulation?
- What is the difference between overloading and overriding?
- What types of inheritance exist and does Java support multiple inheritance?
- Can you have OOP without inheritance?
MCQ Practice
1. Which is NOT one of the four pillars of OOP?
Recursion is a technique, not an OOP pillar. The four are encapsulation, abstraction, inheritance, polymorphism.
2. Reusing a parent class’s members in a child class is?
Inheritance derives fields and behavior from a parent class.
3. Hiding internal state and exposing methods is?
Encapsulation bundles data with methods and restricts direct access.
Flash Cards
Pillar 1: Encapsulation? — Bundle data with methods and restrict direct access to internal state.
Pillar 2: Abstraction? — Expose essential behavior; hide implementation complexity.
Pillar 3: Inheritance? — Derive a class from a parent to reuse and specialise its members.
Pillar 4: Polymorphism? — Different classes respond to the same method call in their own way.