Object-Oriented Programming (OOPs)
Object-Oriented Programming (OOP) is a programming paradigm based on the
concept of "objects," which can contain data (attributes) and code (methods). It helps
in structuring programs for better reusability, scalability, and modularity.
Key Principles of OOP
1. Encapsulation – Wrapping data and methods into a single unit (class) to
restrict direct access.
2. Abstraction – Hiding implementation details and showing only necessary
functionality.
3. Inheritance – Allowing a class to inherit properties and behavior from
another class.
4. Polymorphism – The ability of different objects to respond differently to the
same function call.
Example in Python
# Encapsulation
class Car:
def __init__(self, brand, speed):
self.brand = brand # Public attribute
self.__speed = speed # Private attribute (Encapsulation)
def get_speed(self):
return self.__speed # Getter method
# Inheritance
class ElectricCar(Car):
def charge(self):
print(f"{self.brand} is charging.")
# Polymorphism
class PetrolCar(Car):
def refuel(self):
print(f"{self.brand} is refueling.")
# Object Creation
tesla = ElectricCar("Tesla", 150)
bmw = PetrolCar("BMW", 180)
# Accessing Methods
print(tesla.get_speed()) # Encapsulation
tesla.charge() # Inheritance
bmw.refuel() # Polymorphism
OOP Benefits
✅ Code Reusability – Reduces redundancy
✅ Modularity – Easier to maintain and update
✅ Scalability – Suitable for large applications
✅ Security – Protects data using encapsulation