The four main principles of Object-Oriented Programming (OOP) are Encapsulation, Abstraction, Inheritance, and Polymorphism. These principles help organize code around objects, making it modular, reusable, and easier to maintain. Here’s a detailed, interview-ready explanation with examples in Python:
1. Encapsulation #
- Definition: Bundling data (attributes) and methods (functions) that operate on that data into a single unit (class), and restricting access to some parts of the object to prevent unintended interference.
- Purpose: Protects internal state and ensures controlled access.
Example:
filename.python
class Employee:
def __init__(self, name, salary):
self.name = name # public
self.__salary = salary # private
def get_salary(self):
return self.__salary
emp = Employee("Aswini", 5000)
print(emp.get_salary()) # Access private attribute safely
2. Abstraction #
- Definition: Hiding the internal implementation details and exposing only the necessary functionalities.
- Purpose: Simplifies interaction and reduces complexity for users.
Example:
filename.python
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
# Users interact only via deposit() and get_balance()
3. Inheritance #
- Definition: Allows a class (child/subclass) to inherit attributes and methods from another class (parent/superclass).
- Purpose: Promotes code reuse and hierarchical relationships.
Example:
filename.python
class Employee:
def work(self):
print("Employee working")
class Manager(Employee): # Inherits Employee
def manage(self):
print("Manager managing team")
mgr = Manager()
mgr.work() # inherited method
mgr.manage() # own method
4. Polymorphism #
- Definition: Ability of objects to take many forms; same operation can behave differently on different classes.
- Types: Method overriding (runtime polymorphism), operator overloading, or method overloading (Python supports via default arguments).
Example (Method Overriding):
filename.python
class Employee:
def work(self):
print("Employee working")
class Manager(Employee):
def work(self):
print("Manager managing team") # overrides work()
emp = Employee()
mgr = Manager()
emp.work() # Employee working
mgr.work() # Manager managing team
Summary Table #
| Principle | What it Means | Purpose |
|---|---|---|
| Encapsulation | Data + methods in a single class | Protect data, control access |
| Abstraction | Hide internal details, show functionality | Simplify interface |
| Inheritance | Child class inherits parent class | Reuse code, create hierarchy |
| Polymorphism | Same method behaves differently | Flexibility, dynamic behavior |
In real projects, I’ve used these principles extensively—for example, in a reporting system, I used:
- Encapsulation to protect sensitive data like report templates,
- Abstraction to expose only methods like
generate_report(), - Inheritance for different report types, and
- Polymorphism to let all reports implement
generate_report()differently.
This made the system modular, reusable, and maintainable.
