0% found this document useful (0 votes)
3 views

Py Assignment 6 S11 16

The document explains polymorphism in Python, focusing on operator overloading and method overriding. It provides examples of shape area calculations and employee management systems, demonstrating how subclasses can implement their own methods. The conclusion highlights the successful demonstration of polymorphism in various programming scenarios.

Uploaded by

pranshu07d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Py Assignment 6 S11 16

The document explains polymorphism in Python, focusing on operator overloading and method overriding. It provides examples of shape area calculations and employee management systems, demonstrating how subclasses can implement their own methods. The conclusion highlights the successful demonstration of polymorphism in various programming scenarios.

Uploaded by

pranshu07d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

THADOMAL SHAHANI ENGINEERING COLLEGE

DEPARTMENT OF INFORMATION TECHNOLOGY

6. Polymorphism using operator overloading, method


overriding: LO1, LO3

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:

Operator overloading allows built-in operators to be redefined for user-


defined objects. In Python, special methods like __add__, __sub__, and
__mul__ are used for this purpose.

Method Overriding:

Method overriding allows a subclass to provide its own specific


implementation of a method that is already defined in its superclass. The
overridden method must have the same name, return type, and parameters
as the method in the parent class. This feature ensures that different classes
implement the same method in a way suited to their unique behavior.

Unlike other programming languages, Python does not support method


overloading in the conventional sense. Instead, Python allows a single
function to accept different numbers of arguments by using default values or
variable-length arguments (*args, **kwargs).
Program with output:

1. Shape Polymorphism (Method Overloading &


Overriding):
Method Overriding:
 The area() method in the Shape class is overridden in Circle,
Rectangle, and Triangle subclasses to provide their respective area
calculations.

Program:
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()

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:

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("Vedant", 20000, 6)

emp2 = Manager("A", 70000, 10)

emp3 = Engineer("B", 50000, "Software Development")

emp1.show_details()

emp2.show_details()

emp3.show_details()

Output:
3. Banking Interface: (Operator Overloading):
Operator Overloading (+ and - operators):

o The __add__ method is overloaded to allow deposits using


account + amount.
o The __sub__ method is overloaded to allow withdrawals using
account - amount.

Program:
class BankAccount:

def __init__(self, account_holder, balance=0):

self.account_holder = account_holder

self.balance = balance

def deposit(self, amount):

self.balance += amount

print(f"Deposited {amount}. New balance: {self.balance}")

def withdraw(self, amount):


if self.balance >= amount:

self.balance -= amount

print(f"Withdrawn {amount}. Remaining balance: {self.balance}")

else:

print("Insufficient balance")

def __add__(self, amount):

self.deposit(amount)

return self

def __sub__(self, amount):

self.withdraw(amount)

return self

def display_balance(self):

print(f"Account holder: {self.account_holder}, Balance: {self.balance}")

def main():

account = BankAccount("Vedant", 0)

while True:

print("\n1. Deposit\n2. Withdraw\n3. Show Balance\n4. Exit")

choice = int(input("Enter choice: "))

if choice == 1:

amount = float(input("Enter amount to deposit: "))

account + amount

elif choice == 2:

amount = float(input("Enter amount to withdraw: "))

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.

You might also like