0% found this document useful (0 votes)
16 views4 pages

Lab4 handout (1)

This document is a lab handout for a course on Object Oriented Programming, focusing on the concept of Polymorphism in Python. It explains polymorphism through various examples, including built-in functions, user-defined functions, class methods, and inheritance with method overriding. The document also includes tasks for students to create class hierarchies and demonstrate polymorphism in their code.

Uploaded by

thewriterdeenan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Lab4 handout (1)

This document is a lab handout for a course on Object Oriented Programming, focusing on the concept of Polymorphism in Python. It explains polymorphism through various examples, including built-in functions, user-defined functions, class methods, and inheritance with method overriding. The document also includes tasks for students to create class hierarchies and demonstrate polymorphism in their code.

Uploaded by

thewriterdeenan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Institute of Biomedical Engineering and Technology

Liaquat University of Medical and Health Sciences Jamshoro, Sindh.

Subject: Object Oriented Programming Year: 1st, Semester: 2nd


Lab Handout-04

Name: ________________________________ Roll No: ________________________

Date: _________________ Signature of Tutor: __________________

Introduction to Polymorphism in Python

Objectives:
1. To understand the concept of Polymorphism in Python

Polymorphism in Python

The word polymorphism means having many forms. In simple words Polymorphism is
defined as the ability of a message to be displayed in more than one form. Real life example
of polymorphism is a person at the same time can have different characteristic. Like a man at
the same time is a father, a husband, an employee. In programming, polymorphism means the
same function name but being used for different types. The key difference is the data types
and number of arguments used in function.
Polymorphism allows a single function, method, or operator to work with different types of
data or objects. It allows objects of different types to be treated as objects of a common type,
which simplifies code and enhances flexibility.

Example of inbuilt polymorphic functions:

# len() being used for a string


print(len("Object Oriented Programming"))

# len() being used for a list


print(len([10, 20, 30]))

Examples of user-defined polymorphic functions:

def add(x, y, z = 0):


return x + y + z

# Driver code
print(add(2, 3))
print(add(2, 3, 4))

Polymorphism with class methods:

The below code shows how Python can use two different class types, in the same way. We
create a for loop that iterates through a tuple of objects. Then call the methods without
being concerned about which class type each object is. We assume that these methods
actually exist in each class.

class Pakistan():
def capital(self):
print("Islamabad is the capital of Pakistan.")

def language(self):
print("Urdu is the most widely spoken language of Pakistan.")

def type(self):
print("Pakistan is a developing country.")

class Palestine():
def capital(self):
print("Jerusalem is the capital of Palestine.")

def language(self):
print("Arabic is the primary language of Palestine.")

def type(self):
print("Palestine is a besieged country.")

obj_pak = Pakistan()
obj_pal = Palestine()
for country in (obj_pak, obj_pal):
country.capital()
country.language()
country.type()

Polymorphism in Classes

class LUMHS:
def info(self):
name = "YOUR NAME"
dep = "BIOMEDICAL ENGINEERING"
print(name + " from "+dep)
class MUET:
def info(self):
name = "YOUR FRIEND’S NAME"
dep = "SOFTWARE ENGINEERING"
print(name + " from "+dep)
obj_lumhs = LUMHS()
obj_muet = MUET()
obj_lumhs.info()
obj_muet.info()
Explanation:

In the above example, we have created two classes LUMHS and MUET. These two
different classes have the same method name info(). This method contains
information(name, department). Imagine if there were many hundreds of classes having
methods with different name. Then the developer has to remember all of the methods name
separately. This is where polymorphism comes to the rescue. Python allows methods of
same name in different classes. Then after initializing classes, we created two objects for
respective classes. Then the method info() is called. Once by the object of LUMHS class
and once by the object of MUET class.

Polymorphism with Inheritance

Polymorphism in python defines methods in the child class that have the same name as the
methods in the parent class. In inheritance, the child class inherits the methods from the
parent class. Also, it is possible to modify a method in a child class that it has inherited
from the parent class.
This is mostly used in cases where the method inherited from the parent class doesn’t fit the
child class. This process of re-implementing a method in the child class is known as
Method Overriding. Here is an example that shows polymorphism with inheritance.

Example

class Bird:
def intro(self):
print("There are different types of birds")

def flight(self):
print("Most of the birds can fly but some cannot")

class parrot(Bird):
def flight(self):
print("Parrots can fly")

class penguin(Bird):
def flight(self):
print("Penguins do not fly")

obj_bird = Bird()
obj_parr = parrot()
obj_peng = penguin()

obj_bird.intro()
obj_bird.flight()

obj_parr.intro()
obj_parr.flight()

obj_peng.intro()
obj_peng.flight()
Explanation

The Bird class has an intro method that provides general information about birds and a
flight method that mentions the general flight capability of birds.
The parrot class is a subclass of Bird. It overrides the flight method to provide specific
information about parrots' ability to fly.
The penguin class is another subclass of Bird. It also overrides the flight method to state
that penguins do not fly. Instances of each class (obj_bird, obj_parr, and obj_peng) are
created. When you call the intro and flight methods on these instances, the methods from
the respective classes are executed.

Tasks:
1. Create a class hierarchy for employees, including classes for Manager, Engineer, and
Salesperson. Define methods for each class that represent their specific roles, such as
assign_tasks() for Manager, design_software() for Engineer, and negotiate_deals() for
Salesperson.

2. Create a python code that explains polymorphism using inheritance and method
overriding.

You might also like