Object-Oriented Programming (OOP) in Python – Explanation
Object-Oriented Programming (OOP) is a programming paradigm that organizes software
design around objects rather than functions. Objects represent real-world entities and help
make code reusable, modular, and scalable.
1. What is a Class and an Object?
A class is a blueprint for creating objects. It defines attributes (variables) and
methods (functions).
An object is an instance of a class with specific values for attributes.
Example:
python
CopyEdit
class Car:
def __init__(self, brand, model): # Constructor
self.brand = brand # Instance variable
self.model = model
def display_info(self): # Method
print(f"Car: {self.brand} {self.model}")
# Creating objects (instances of the Car class)
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
car1.display_info() # Output: Car: Toyota Corolla
car2.display_info() # Output: Car: Honda Civic
📌 Explanation:
Car is a class with attributes brand and model.
__init__ is a constructor, automatically called when an object is created.
car1 and car2 are objects with different values for brand and model.
2. Encapsulation (Data Hiding)
Encapsulation restricts direct access to an object's data and only allows modifications
through methods.
Example:
python
CopyEdit
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
# Creating an object
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance()) # Output: 1500
📌 Explanation:
__balance is a private variable (cannot be accessed directly).
deposit() method updates the balance, and get_balance() retrieves it.
3. Inheritance (Code Reusability)
Inheritance allows a class to acquire properties and methods from another class.
Example:
python
CopyEdit
class Animal:
def speak(self):
print("This animal makes a sound")
class Dog(Animal): # Dog class inherits from Animal
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Dog barks
📌 Explanation:
Dog inherits from Animal, meaning it gets all the attributes and methods of Animal.
The speak() method is overridden in the Dog class.
4. Polymorphism (Same Method, Different Behavior)
Polymorphism allows different classes to have methods with the same name but different
behaviors.
Example:
python
CopyEdit
class Cat:
def speak(self):
print("Meow")
class Dog:
def speak(self):
print("Bark")
# Function using polymorphism
def make_sound(animal):
animal.speak()
cat = Cat()
dog = Dog()
make_sound(cat) # Output: Meow
make_sound(dog) # Output: Bark
📌 Explanation:
Both Cat and Dog have a speak() method, but they behave differently.
make_sound() function works for both classes.
5. Abstraction (Hiding Implementation)
Abstraction hides unnecessary details and only shows the essential features.
Example using ABC (Abstract Base Class):
python
CopyEdit
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass # Abstract method, must be implemented in child class
class Car(Vehicle):
def start(self):
print("Car starts with a key")
car = Car()
car.start() # Output: Car starts with a key
📌 Explanation:
Vehicle is an abstract class (cannot be instantiated).
start() is an abstract method (must be implemented in subclasses).
Advantages of OOP in Python
✅ Code Reusability – Inheritance allows using existing code.
✅ Modularity – Code is organized into separate classes.
✅ Encapsulation – Data is protected from direct modification.
✅ Scalability – Easy to maintain and extend code.