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([Link]) # 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):
[Link] = name
[Link] = age
s1 = Student("John", 20)
print([Link]) # Output: John
Attributes
Variables that belong to the object.
python
[Link] # '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):
[Link] = name
def greet(self):
print("Hello", [Link])
s1 = Student("John")
[Link]() # 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()
[Link]() # Inherited from Animal
[Link]() # 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()]:
[Link]()
🔸 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):
[Link] = brand
[Link] = model
[Link] = price
def call(self, number):
print(f"Calling {number} from {[Link]}...")
def show_info(self):
print(f"{[Link]} {[Link]} costs ₹{[Link]}")
phone1 = MobilePhone("Samsung", "Galaxy S21", 69999)
phone2 = MobilePhone("Apple", "iPhone 13", 79999)
[Link]("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):
[Link] = name
[Link] = marks
def display(self):
print(f"Name: {[Link]}, Marks: {[Link]}")
def result(self):
if [Link] >= 40:
print("Result: Pass")
else:
print("Result: Fail")
# Test
s1 = Student("John", 75)
s2 = Student("Sara", 35)
[Link]()
[Link]()
[Link]()
[Link]()
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):
[Link] = name
[Link] = salary
def show(self):
print(f"Employee Name: {[Link]}, Salary: ₹{[Link]}")
# Test
emp1 = Employee("Aseena", 40000)
emp2 = Employee("Rahul", 55000)
[Link]()
[Link]()