key concepts of OOP
The Four Pillars of Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is built on four fundamental concepts known as the Four Pillars of OOP.
These pillars help us write:
- Reusable code
- Organized code
- Maintainable applications
- Scalable software
The Four Pillars
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
Overview Diagram
OOP
│
┌────────────┼────────────┐
│ │ │
Encapsulation Inheritance Polymorphism
│
Abstraction
Or more commonly:
OOP
│
┌─────────────────────────────────┐
│ │
├── Encapsulation
├── Inheritance
├── Polymorphism
└── Abstraction
1. Encapsulation
Definition
Encapsulation means:
Hiding internal data and providing controlled access through methods.
It combines:
Data + Methods
inside a class.
Example
class BankAccount:
def __init__(self):
self.__balance = 1000
def get_balance(self):
return self.__balance
Usage:
account = BankAccount()
print(account.get_balance())
Goal
Protect data from accidental modification.
Without Encapsulation:
account.balance = -10000
With Encapsulation:
account.withdraw(100)
Validation becomes possible.
Real-Life Example
ATM Machine
You can:
Withdraw Money
Deposit Money
You cannot:
Directly access bank servers
2. Inheritance
Definition
Inheritance allows one class to acquire properties and methods from another class.
In simple words:
Parent Class
↓
Child Class
The child reuses code from the parent.
Example
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
pass
Usage:
dog = Dog()
dog.eat()
Output:
Eating
Why Use Inheritance?
Without inheritance:
class Dog:
def eat(self):
pass
class Cat:
def eat(self):
pass
Duplicate code.
With inheritance:
class Animal:
def eat(self):
pass
Reuse code.
Real-Life Example
Animal
│
├── Dog
├── Cat
└── Lion
All animals can:
Eat
Sleep
Breathe
No need to rewrite these methods.
3. Polymorphism
Definition
Polymorphism means:
One interface, many implementations.
The same method name can behave differently in different classes.
Example
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
Usage:
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
Output:
Bark
Meow
Why Is This Powerful?
The code:
animal.sound()
works for:
Dog
Cat
Lion
Tiger
Bird
even though each class has a different implementation.
Real-Life Example
Remote Control
│
▼
Turn On
Different devices react differently:
TV → Turns On
Fan → Starts Rotating
AC → Starts Cooling
Same action.
Different behavior.
This is Polymorphism.
4. Abstraction
Definition
Abstraction means:
Showing only essential features while hiding implementation details.
Users see:
What it does
instead of:
How it does it
Example
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
Child class:
class Dog(Animal):
def sound(self):
print("Bark")
Why Use Abstraction?
The parent class defines:
Every animal must have sound()
but doesn't care how it is implemented.
Each child class decides.
Real-Life Example
Car
Driver knows:
Steering
Brake
Accelerator
Driver does NOT need to know:
Fuel Injection
Engine Timing
Gear Mechanics
The complexity is hidden.
Comparison of All Four Pillars
| Pillar | Purpose |
|---|---|
| Encapsulation | Protect Data |
| Inheritance | Reuse Code |
| Polymorphism | Same Interface, Different Behavior |
| Abstraction | Hide Complexity |
Quick Examples
Encapsulation
self.__balance
Hide data.
Inheritance
class Dog(Animal)
Reuse code.
Polymorphism
animal.sound()
Different implementations.
Abstraction
@abstractmethod
def sound(self):
pass
Define rules.
Visual Summary
Encapsulation
↓
Protect Data
Inheritance
↓
Reuse Code
Polymorphism
↓
Many Forms
Abstraction
↓
Hide Complexity
Easy Way to Remember
Encapsulation
↓
Protect
Inheritance
↓
Reuse
Polymorphism
↓
Different Behaviors
Abstraction
↓
Hide Details
Interview Answer
What are the Four Pillars of OOP?
The Four Pillars of OOP are:
- Encapsulation – Hiding data and controlling access.
- Inheritance – Reusing code from existing classes.
- Polymorphism – Same interface with different implementations.
- Abstraction – Hiding implementation details and showing only essential functionality.
These pillars help create modular, reusable, maintainable, and scalable software systems.