Object-Oriented Programming
Master all four pillars of OOP — Encapsulation, Inheritance, Polymorphism, and Abstraction with practical examples.
Introduction to OOP
OOP is a programming style that organizes code using objects and classes. It models real-world things using programming structures — used in software development, game development, web frameworks, and system design.
Protect Data
Bundling data and methods together, restricting direct access to internal state.
Reuse Code
A child class acquires properties and methods from a parent class.
Many Behaviors
One interface, many forms. Same method name behaves differently depending on the object.
Hide Complexity
Shows only essential features while hiding implementation details.
Classes & Objects
A class is a blueprint. An object is a real-world entity created from that blueprint.
class Student: def __init__(self, name, age, marks): self.name = name self.age = age self.marks = marks def display(self): print(f"Name: {self.name}, Age: {self.age}") def is_passed(self): return self.marks >= 40 s1 = Student("Alice", 20, 85) s2 = Student("Bob", 22, 35) s1.display() # Name: Alice, Age: 20 print("Passed:", s1.is_passed()) # Passed: True print("Passed:", s2.is_passed()) # Passed: False
self.attr = value.self.Instance, Class & Static Methods
Python classes support three types of methods. Quick memory trick: I C S — Instance / Class / Static.
class Student: school_name = "ABC School" # Class variable def __init__(self, name, marks): self.name = name self.marks = marks def display(self): # Instance method print(self.name, self.marks) @classmethod def get_school(cls): # Class method return cls.school_name @staticmethod def grade(marks): # Static method (utility) return "A" if marks >= 90 else "B"
Quick Memory Trick — I C S
Encapsulation
Encapsulation means bundling data and methods together and restricting direct access.
| Access Type | Convention | Example | Accessible From |
|---|---|---|---|
| Public | No prefix | self.name | Anywhere |
| Protected | Single underscore | self._name | Internal use (convention) |
| Private | Double underscore | self.__name | Class only (name mangling) |
class BankAccount: def __init__(self, balance): self.__balance = balance # Private variable def deposit(self, amount): if amount > 0: self.__balance += amount def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount def get_balance(self): # Getter method return self.__balance
Inheritance
Inheritance allows a child class to reuse properties and methods from a parent class. Use super() to call parent methods.
Single
One parent → One child
Multiple
Child inherits from multiple parents
Multilevel
An inheritance chain
Hierarchical
Multiple children from one parent
class Animal: def speak(self): print("Animal makes a sound") class Dog(Animal): def speak(self): # Override parent method super().speak() # Call parent first print("Dog barks")
Polymorphism
Polymorphism = one interface, many behaviors. Achieved through method overriding, operator overloading, and duck typing.
# Method overriding polymorphism class Dog(Animal): def speak(self): print("Dog barks") class Cat(Animal): def speak(self): print("Cat meows") for a in [Dog(), Cat()]: a.speak() # Same call, different behavior # Operator overloading print(5 + 3) # 8 (addition) print("Hi " + "There") # string concat
Abstraction
Abstraction means hiding complex implementation details using the abc module with Abstract Classes.
from abc import ABC, abstractmethod class Shape(ABC): # Abstract class @abstractmethod def area(self): pass # Must be implemented class Rectangle(Shape): def __init__(self, w, h): self.w = w; self.h = h def area(self): return self.w * self.h r = Rectangle(4, 5) print(r.area()) # 20
Magic Methods (Dunder)
Magic methods start and end with double underscores. They let objects behave like built-in types.