OOP in Python - Study Handout
1. Class & Object
● Class: Blueprint for creating objects.
● Object: Instance of a class.
class Car:
def __init__(self, brand, color, model):
self.brand = brand # Attribute
self.color = color
self.model = model
store = Car("Tesla", "Red", "Y") # Instance (Object)
2. Attributes vs Instances
● brand is an attribute.
● self.brand is the instance-specific version of that attribute.
3. Encapsulation
● Definition: Bundling data and methods inside a class and restricting direct access to
internal details.
● Benefit: Protects data from outside interference.
class Student:
def __init__(self, name, score=0):
self.name = name
self.__score = score # Private attribute
def add_score(self, score):
if score > 0:
self.__score += score
def reset_score(self, password):
if password == "admin":
self.__score = 0
else:
return "Wrong password"
def get_score(self):
return self.__score
4. Inheritance
● Definition: Deriving a new class (child) from an existing class (parent).
● Benefit: Reusability and cleaner code.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def show_info(self):
return f"Name: {self.name} - Salary: ${self.salary}"
class Manager(Employee):
def __init__(self, name, salary, department):
super().__init__(name, salary)
self.department = department
def show_info(self):
return f"Name: {self.name} - Department: {self.department} - Salary: ${self.salary}"
5. Polymorphism
● Definition: Different classes can have the same method name but behave differently.
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 22/7 * self.radius ** 2
# Polymorphism in action:
shapes = [Rectangle(4, 5), Circle(7)]
for shape in shapes:
print(shape.area()) # Same method, different behavior
6. Abstraction
● Definition: Hiding implementation details and exposing only the interface.
● Achieved using the abc module and @abstractmethod.
from abc import ABC, abstractmethod
class PaymentMethod(ABC):
@abstractmethod
def pay(self):
pass
class Paypal(PaymentMethod):
def __init__(self, amount):
self.amount = amount
def pay(self):
return f"Paid ${self.amount} through Paypal"
class BankTransfer(PaymentMethod):
def __init__(self, amount):
self.amount = amount
def pay(self):
return f"Paid ${self.amount} through Bank Transfer"
If a class inherits from PaymentMethod and does not implement pay(), it will raise an error —
enforcing abstraction.
Summary Table
Principle Meaning Benefit
Encapsulation Hide internal data with access via methods Data protection
Inheritance Derive new classes from existing ones Code reusability
Polymorphism Use same method names in different classes Flexible code
Abstraction Hiding internal logic and exposing only the Enforced structure and
interface clarity
Final Tip:
Use all four OOP pillars to write maintainable, modular, and scalable applications.