Py Assignment 6 S11 16
Py Assignment 6 S11 16
Aim:
To demonstrate Polymorphism in Python
Theory:
Polymorphism:
Polymorphism is a fundamental concept in object-oriented programming that
allows a single interface to be used for different data types. It enables a
method to perform different tasks based on the object that is calling it. This
feature promotes code reusability and flexibility by allowing objects of
different classes to be treated as objects of a common superclass.
Operator Overloading:
Method Overriding:
Program:
class Shape:
def area(self):
class Circle(Shape):
self.radius = radius
def area(self):
class Rectangle(Shape):
self.length = length
self.breadth = breadth
def area(self):
class Triangle(Shape):
self.height = height
def area(self):
def main():
while True:
if choice == 1:
Circle(radius).area()
elif choice == 2:
Rectangle(length, breadth).area()
elif choice == 3:
Triangle(base, height).area()
elif choice == 4:
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()
Output:
2. Employee Management (Method Overriding):
Method Overriding:
The show_details() method is overridden in the Intern, Manager, and
Engineer subclasses to provide specialized implementations.
Program:
class Employee:
self.name = name
self.salary = salary
def show_details(self):
class Intern(Employee):
super().__init__(name, salary)
self.duration = duration
def show_details(self):
class Manager(Employee):
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):
super().__init__(name, salary)
self.domain = domain
def show_details(self):
emp1.show_details()
emp2.show_details()
emp3.show_details()
Output:
3. Banking Interface: (Operator Overloading):
Operator Overloading (+ and - operators):
Program:
class BankAccount:
self.account_holder = account_holder
self.balance = balance
self.balance += amount
self.balance -= amount
else:
print("Insufficient balance")
self.deposit(amount)
return self
self.withdraw(amount)
return self
def display_balance(self):
def main():
account = BankAccount("Vedant", 0)
while True:
if choice == 1:
account + amount
elif choice == 2:
account - amount
elif choice == 3:
account.display_balance()
elif choice == 4:
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()
Output:
Conclusion:
Hence, polymorphism was demonstrated through operator overloading in the
banking system, and method overriding in the employer management
system and shape calculations.