Module 8: Object-Oriented Programming
in Python
What is OOP?
Object-Oriented Programming is a programming paradigm based on
the concept of objects, which contain data (attributes) and functions
(methods). Python supports OOP and makes it simple to use.
1. Classes and Objects
Class
A class is a blueprint for creating objects. It defines attributes and
methods.
python
class Student:
name = "John"
age = 20
Object
An object is an instance of a class.
python
s1 = Student()
print(s1.name) # Output: John
Important: Multiple objects can be created from one class.
2. Constructors, Attributes, and Methods
Constructor (__init__)
A special method that runs when an object is created. It initializes the
object’s attributes.
python
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
s1 = Student("John", 20)
print(s1.name) # Output: John
Attributes
Variables that belong to the object.
python
s1.name # 'John' is an attribute of object s1
Methods
Functions defined inside a class that operate on its attributes.
python
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
s1 = Student("John")
s1.greet() # Output: Hello John
🔸 Tip: Use self to refer to the current object inside methods.
3. Inheritance
Inheritance allows one class (child) to inherit attributes and methods
from another class (parent).
Example:
python
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak() # Inherited from Animal
d.bark() # Defined in Dog
🔸 Key Point: Dog inherits from Animal using class Dog(Animal):
4. Polymorphism
Polymorphism means "many forms" — the same method name
behaves differently depending on the object.
Example:
python
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
for animal in [Cat(), Dog()]:
animal.sound()
🔸 Key Point: Both classes have a sound() method, but output differs.
Summary of Key Terms
Term Meaning
Class Blueprint for creating objects
Object Instance of a class
Constructor __init__ method to initialize attributes
Attribute Variable inside a class
Method Function inside a class
Inheritance One class inherits from another
Polymorphism Same method name, different behavior
class MobilePhone:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
def call(self, number):
print(f"Calling {number} from {self.model}...")
def show_info(self):
print(f"{self.brand} {self.model} costs ₹{self.price}")
phone1 = MobilePhone("Samsung", "Galaxy S21", 69999)
phone2 = MobilePhone("Apple", "iPhone 13", 79999)
phone1.call("9876543210")
phone2.show_info()
Q.1 Write a Python program to create a Student class with attributes name and marks.
Add methods:
display()
result() → Print "Pass" if marks >= 40 else "Fail".
Answer:
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def display(self):
print(f"Name: {self.name}, Marks: {self.marks}")
def result(self):
if self.marks >= 40:
print("Result: Pass")
else:
print("Result: Fail")
# Test
s1 = Student("John", 75)
s2 = Student("Sara", 35)
s1.display()
s1.result()
s2.display()
s2.result()
Q.2 Write a Python program to create an Employee class with attributes name and
salary.
Add a method show() to display employee details and create two employee objects.
Answer:
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def show(self):
print(f"Employee Name: {self.name}, Salary: ₹{self.salary}")
# Test
emp1 = Employee("Aseena", 40000)
emp2 = Employee("Rahul", 55000)
emp1.show()
emp2.show()