Introduction to Object-Oriented
Programming in Python
1. Concept of Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm that organizes code
into objects.
An object represents a real-world entity and bundles together:
o Data (attributes/variables)
o Behavior (methods/functions)
Instead of writing separate functions and variables, OOP allows us to group them into
classes and objects for better organization.
💡 Example:
A Car can be represented as an object with:
Data: color, model, speed
Methods: start(), stop(), accelerate()
2. Advantages of OOP
1. Reusability – Code can be reused using classes and inheritance.
2. Modularity – Large problems can be broken into smaller, manageable objects.
3. Maintainability – Easier to update and modify code.
4. Abstraction – Hides unnecessary details and shows only important features.
5. Encapsulation – Keeps data safe by restricting direct access.
6. Polymorphism – Same function name can be used in different ways.
3. Basic Elements of OOP
a. Class
A class is a blueprint or template for creating objects.
It defines attributes (variables) and methods (functions).
class Car:
def __init__(self, color, model):
self.color = color
self.model = model
def start(self):
print("Car has started")
b. Object
An object is an instance of a class.
Multiple objects can be created from a single class.
my_car = Car("Red", "Tesla Model 3")
print(my_car.color) # Output: Red
my_car.start() # Output: Car has started
c. Encapsulation
Binding of data and methods into a single unit (class).
Data can be made private so that it cannot be accessed directly.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private variable
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
d. Polymorphism
One name, many forms → Same method name can behave differently depending on the
context.
Example – Method Overriding:
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
print("Bark")
dog = Dog()
dog.sound() # Output: Bark
e. Inheritance
Allows one class (child) to acquire properties and methods of another (parent).
class Vehicle:
def move(self):
print("Vehicle is moving")
class Car(Vehicle): # Car inherits from Vehicle
def horn(self):
print("Beep Beep")
c = Car()
c.move() # Output: Vehicle is moving
c.horn() # Output: Beep Beep
ypes of Inheritance in Python
Inheritance is one of the main features of Object-Oriented Programming (OOP).
It allows a class (child/derived class) to use the properties and methods of another class
(parent/base class).
This avoids code repetition and makes programs easier to maintain.
1. Single Inheritance
👉 In this type, a child class inherits from only one parent class.
Diagram:
Parent → Child
Example:
class Parent:
def display(self):
print("This is the Parent class")
class Child(Parent): # inherits Parent
def show(self):
print("This is the Child class")
obj = Child()
obj.display() # from Parent
obj.show() # from Child
2. Multiple Inheritance
👉 A child class inherits from more than one parent class.
Diagram:
Parent1 Parent2
\ /
Child
Example:
class Father:
def quality(self):
print("Father is hardworking")
class Mother:
def nature(self):
print("Mother is caring")
class Child(Father, Mother): # inherits both
def own(self):
print("Child is talented")
obj = Child()
obj.quality() # from Father
obj.nature() # from Mother
obj.own() # from Child
3. Multilevel Inheritance
👉 In this type, inheritance happens in a chain, i.e. a child becomes parent for another class.
Diagram:
Grandparent → Parent → Child
Example:
class Grandparent:
def old_property(self):
print("Grandparent’s house")
class Parent(Grandparent):
def middle_property(self):
print("Parent’s car")
class Child(Parent): # inherits Parent (and also Grandparent)
def new_property(self):
print("Child’s laptop")
obj = Child()
obj.old_property()
obj.middle_property()
obj.new_property()
4. Hierarchical Inheritance
👉 In this type, multiple child classes inherit from a single parent class.
Diagram:
Parent
/ \
Child1 Child2
Example:
class Parent:
def message(self):
print("This is the Parent class")
class Child1(Parent):
def feature1(self):
print("This is Child1 class")
class Child2(Parent):
def feature2(self):
print("This is Child2 class")
obj1 = Child1()
obj2 = Child2()
obj1.message()
obj1.feature1()
obj2.message()
obj2.feature2()
5. Hybrid Inheritance
👉 A combination of two or more types of inheritance (e.g. multiple + multilevel).
👉 Can sometimes cause confusion, so Python uses MRO (Method Resolution Order) to
decide the order of method execution.
Diagram Example:
A
/ \
B C
\ /
D
Example:
class A:
def show(self):
print("Class A")
class B(A):
def showB(self):
print("Class B")
class C(A):
def showC(self):
print("Class C")
class D(B, C): # Hybrid inheritance
def showD(self):
print("Class D")
obj = D()
obj.show() # Python follows MRO (A → B → C → D)
obj.showB()
obj.showC()
obj.showD()
Summary Table
Type of Inheritance Description Example
Single One child, one parent Child(Parent)
Multiple One child, many parents Child(Father, Mother)
Multilevel Inherited in chain Grandparent → Parent → Child
Hierarchical One parent, many children Parent → Child1, Child2
Hybrid Mix of two or more types A → B, C → D
f. Data Abstraction
Showing essential details only and hiding internal implementation.
In Python, abstraction is implemented using abstract classes (ABC module).
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r * self.r
g. Data Hiding
Restricting direct access to variables by making them private (using __variable).
Access through methods only (like getters and setters).
4. Access Specifiers in Python
Unlike some languages, Python doesn’t have strict access specifiers, but uses conventions:
1. Public – Accessible from anywhere.
2. self.name = "John" # Public
3. Protected – Denoted by a single underscore _. Accessible within class and subclasses.
4. self._age = 25 # Protected
5. Private – Denoted by double underscore __. Accessible only inside the class.
6. self.__salary = 50000 # Private
5. Constructors
A constructor is a special method (__init__) that initializes object properties when an
object is created.
It is automatically called.
class Student:
def __init__(self, name, age): # constructor
self.name = name
self.age = age
Types of Constructors
1. Default Constructor – Takes no arguments.
2. class A:
3. def __init__(self):
4. print("Default constructor called")
5. Parameterized Constructor – Accepts arguments for initialization.
6. class A:
7. def __init__(self, x):
8. self.x = x
2. Parameterized Constructor in Python
Definition
A parameterized constructor is a constructor that takes arguments from the user while creating
an object.
In Python, the constructor is defined using the special method __init__().
By passing parameters to __init__(), we can initialize objects with different values.
Syntax
class ClassName:
def __init__(self, parameter1, parameter2, ...):
self.parameter1 = parameter1
self.parameter2 = parameter2
...
Example 1: Student Class
class Student:
def __init__(self, name, age): # parameterized constructor
self.name = name
self.age = age
def display(self):
print("Name:", self.name)
print("Age:", self.age)
# Creating objects with different values
s1 = Student("Ali", 20)
s2 = Student("Sara", 22)
s1.display()
s2.display()
✅ Output:
Name: Ali
Age: 20
Name: Sara
Age: 22
Here, the constructor takes two parameters (name, age) and initializes different objects with
different data.
6. Destructor
A destructor is a special method (__del__) that is automatically called when an object is
deleted.
It is mainly used for cleanup activities.
class Test:
def __init__(self):
print("Object created")
def __del__(self):
print("Object destroyed")
obj = Test()
del obj # Destructor called automatically