The Four Pillars of Object-Oriented Programming
Encapsulation, abstraction, inheritance, and polymorphism are the four core principles of OOP. Learn what each one means with simple explanations.
Published September 22, 2026
The four pillars of OOP are encapsulation, abstraction, inheritance and polymorphism. Together they help you model software as objects that hide details, reuse behaviour and work through shared interfaces.
The concept
Encapsulation bundles data with the methods that operate on it and hides internals behind a public interface. Abstraction exposes only what a user needs.
Inheritance lets a class reuse and extend another. Polymorphism lets different classes respond to the same call in their own way.
Polymorphism in one picture
Practical examples
Encapsulation
class Account:
def __init__(self): self._balance = 0
def deposit(self, n):
if n > 0: self._balance += n
@property
def balance(self): return self._balanceInheritance + polymorphism
class Shape:
def area(self): raise NotImplementedError
class Square(Shape):
def __init__(self, s): self.s = s
def area(self): return self.s ** 2
class Circle(Shape):
def __init__(self, r): self.r = r
def area(self): return 3.14159 * self.r ** 2
for s in (Square(2), Circle(1)): print(s.area())Abstraction
from abc import ABC, abstractmethod
class Storage(ABC):
@abstractmethod
def save(self, data): ...
Common mistakes
- Using inheritance for code reuse where composition fits better.
- Exposing every field publicly.
- Deep inheritance trees that are hard to follow.
Key takeaways
- Encapsulate, abstract, inherit, polymorph.
- Prefer composition over inheritance.
- Program to interfaces.
FAQ
Abstraction vs encapsulation?
Abstraction hides complexity by design; encapsulation hides data by access control.
Does OOP require classes?
Not strictly; JavaScript uses prototypes.