What are the four pillars of OOP?
The four pillars of Object Oriented Programming are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation hides data behind methods, abstraction hides complexity behind a simple interface, inheritance lets one class reuse another, and polymorphism lets the same call behave differently for different objects.
- The four pillars are encapsulation, abstraction, inheritance, and polymorphism.
- Encapsulation hides data, abstraction hides complexity, inheritance reuses code, and polymorphism gives one name many forms.
- The memory aid A PIE helps you recall all four quickly in an interview.
1. Encapsulation
Encapsulation means keeping data and the methods that use it inside one class, and controlling access from the outside. You make fields private and expose safe methods to read or change them. Think of a capsule that protects what is inside.
class BankAccount {
double balance = 0; // hidden
public:
void deposit(double amt) { // controlled access
if (amt > 0) balance += amt;
}
};class BankAccount {
private double balance; // hidden
public void deposit(double amt) { // controlled access
if (amt > 0) balance += amt;
}
}class BankAccount:
def __init__(self):
self.__balance = 0 # hidden
def deposit(self, amt): # controlled access
if amt > 0:
self.__balance += amt2. Abstraction
Abstraction means showing only what a user needs and hiding the messy details. When you drive a car you use the steering wheel and pedals. You do not need to know how the engine burns fuel. In code you expose a simple method and hide the complex logic behind it.
3. Inheritance
Inheritance lets a new class reuse the fields and methods of an existing class. The existing class is the parent and the new one is the child. A Dog class can inherit from an Animal class and automatically get its eat and sleep methods.
class Animal {
public:
void eat() { cout << "eating"; }
};
class Dog : public Animal { // Dog reuses eat()
public:
void bark() { cout << "woof"; }
};class Animal {
void eat() { System.out.println("eating"); }
}
class Dog extends Animal { // Dog reuses eat()
void bark() { System.out.println("woof"); }
}class Animal:
def eat(self):
print("eating")
class Dog(Animal): # Dog reuses eat()
def bark(self):
print("woof")4. Polymorphism
Polymorphism means one name behaving in many forms. The same method call can do different things depending on the object. A draw method on a Shape can draw a circle or a square based on which shape it actually is.
A common trick from interviewers is to ask you to remember all four and give one line for each. Use the memory aid A PIE, which stands for Abstraction, Polymorphism, Inheritance, Encapsulation. Then be ready to give a one example for any pillar they pick.
Frequently asked questions
What is the difference between abstraction and encapsulation?
Abstraction hides complexity and happens at the design level, while encapsulation hides data and happens at the implementation level using access modifiers.
Can you give a real world example of each pillar?
A car pedal is abstraction, a sealed capsule is encapsulation, a child inheriting traits is inheritance, and one draw method drawing different shapes is polymorphism.
Common follow up questions
Related interview questions
Want the full OOP guide?
Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the OOP guide All OOP questions