THADOMAL SHAHANI ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
6. Polymorphism-Operator, Method Overloading ,Overriding :
LO3
Aim: To study Polymorphism using Operator Overloading, Method Overloading
and Method Overriding in python
Theory:
Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of
a common superclass, enabling flexibility and code reusability through
method overriding and method overloading.
Method Overriding:
Method overriding occurs when a subclass provides a specific
implementation of a method already defined in its parent class, maintaining
the same method signature while altering its behavior.
Method Overloading:
Method overloading allows multiple methods in the same class to share the
same name but differ in the number or type of parameters, enabling multiple
ways to perform similar actions within a class.
Program with output:
1. Class Student
class Student:
def __init__(self):
self.marks = {}
subject = input("Enter the first subject: ")
marks = float(input(f"Enter marks for {subject}: "))
self.marks[subject] = marks
def add_subject(self, *subjects):
for sub in subjects:
mark = float(input(f"Enter marks for {sub}: "))
self.marks[sub] = mark
def get_marks(self):
print("\nMarks Data:")
for subject, mark in self.marks.items():
print(f"{subject}: {mark}")
def main():
student = Student()
while True:
more = int(input("\nEnter number of more subjects (0 to stop): "))
if more == 0:
student.get_marks()
break
subjects = [input("Enter subject name: ") for _ in range(more)]
student.add_subject(*subjects)
if __name__ == "__main__":
main()
2. Operator Overloading:
class Pointer:
def __init__(self,x,y):
self.x=x
self.y=y
def __add__(self,other):
return Pointer(self.x+other.x, self.y+other.y)
def __sub__(self, other):
return Pointer(self.x-other.x,self.y-other.y)
def __lt__(self, other):
d1=(self.x**2 + self.y**2)**0.5
d2=(other.x**2 + other.y**2)**0.5
if(d1<d2):
return True
else:
return False
def __gt__(self, other):
d1=(self.x**2 + self.y**2)**0.5
d2=(other.x**2 + other.y**2)**0.5
if(d1>d2):
return True
else:
return False
def __str__(self):
return f"Point is ({self.x},{self.y})"
p1=Pointer(2,3)
p2=Pointer(3,4)
p3=p1+p2
p4=p1-p2
print(f"Point p1 is {p1}")
print(f"Point p2 is {p2}")
print(f"p1+p2 is {p3}")
print(f"p1-p2 is {p4}")
print(f"p1<p2: {p1<p2}")
print(f"p1>p2: {p1>p2}")
3. Method Overriding class Shape:
class Shape:
def area(self):
print("Area calculation is not defined for generic Shape")
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
print(f"Circle Area: {3.14 * self.radius * self.radius}")
class Rectangle(Shape):
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth
def area(self):
print(f"Rectangle Area: {self.length * self.breadth}")
class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height
def area(self):
print(f"Triangle Area: {0.5 * self.base * self.height}")
def main():
while True:
print("Choose the shape to calculate the area\n1. Circle\n2. Rectangle\n3.
Triangle\n4. Exit")
choice = int(input("Enter choice: "))
if choice == 1:
radius = float(input("Enter radius: "))
Circle(radius).area()
elif choice == 2:
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
Rectangle(length, breadth).area()
elif choice == 3:
base = float(input("Enter base: "))
height = float(input("Enter height: "))
Triangle(base, height).area()
elif choice == 4:
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()
4.Method overriding class Employee:
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def show_details(self):
print(f"Employee: {self.name}, Salary: {self.salary}")
class Intern(Employee):
def __init__(self, name, salary, duration):
super().__init__(name, salary)
self.duration = duration
def show_details(self):
print(f"Intern: {self.name}, Salary: {self.salary}, Duration: {self.duration}
months")
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary)
self.team_size = team_size
def show_details(self):
print(f"Manager: {self.name}, Salary: {self.salary}, Team Size:
{self.team_size}")
class Engineer(Employee):
def __init__(self, name, salary, domain):
super().__init__(name, salary)
self.domain = domain
def show_details(self):
print(f"Engineer: {self.name}, Salary: {self.salary}, Department:
{self.domain}")
emp1 = Intern("Tanishk", 70000, 6)
emp2 = Manager("Rohit", 80000, 10)
emp3 = Engineer("Dhruv", 60000, "Software Development")
emp1.show_details()
emp2.show_details()
emp3.show_details()
OUTPUT:
Conclusion: Thus we have implemented polymorphism in python by using
the concepts of method overloading, operator overloading and method
overriding.