0% found this document useful (0 votes)
5 views6 pages

Polymorphism

Polymorphism in Python allows objects to take many forms and perform the same action in different ways, such as through method overriding in inheritance. The built-in function len() exemplifies polymorphism by returning different results based on the object type. Additionally, polymorphism can be applied in class methods, enabling different objects to share the same method names while executing unique implementations.

Uploaded by

rathodevv6
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)
5 views6 pages

Polymorphism

Polymorphism in Python allows objects to take many forms and perform the same action in different ways, such as through method overriding in inheritance. The built-in function len() exemplifies polymorphism by returning different results based on the object type. Additionally, polymorphism can be applied in class methods, enabling different objects to share the same method names while executing unique implementations.

Uploaded by

rathodevv6
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/ 6

 Polymorphism in Python

 Polymorphism in Python is the ability of an object to take many forms.


 In simple words, polymorphism allows us to perform the same action in
many different ways.

For example, Jessa acts as an employee when she is at the office. However,
when she is at home, she acts like a wife. Also, she represents herself differently
in different places. Therefore, the same person takes different forms as per the
situation.

In polymorphism, a method can process objects differently depending on


the class type or data type. Let’s see simple examples to understand it better.

1. Polymorphism in Built-in function len()

 The built-in function len() calculates the length of an object depending


upon its type.

 If an object is a string, it returns the count of characters, and If an object is


a list, it returns the count of items in a list.

Mr. Dhanraj Suresh Kiwde Page 1


 The len() method treats an object as per its class type.

Example:

students = ['Emma', 'Jessa', 'Kelly']

school = 'ABC School'

# calculate count

print(len(students))

print(len(school))

Output

10

Mr. Dhanraj Suresh Kiwde Page 2


2. Polymorphism with Inheritance

1. Polymorphism is mainly used with inheritance.

2. In inheritance, child class inherits the attributes and methods of a parent


class.

3. The existing class is called a base class or parent class, and the new class is
called a subclass or child class or derived class.

4. Using method overriding polymorphism allows us to defines methods in


the child class that have the same name as the methods in the parent class.

5. This process of re-implementing the inherited method in the child


class is known as Method Overriding.

Advantage of method overriding

 It is effective when we want to extend the functionality by altering the


inherited method. Or the method inherited from the parent class doesn’t
fulfill the need of a child class, so we need to re-implement the same
method in the child class in a different way.
 Method overriding is useful when a parent class has multiple child classes,
and one of that child class wants to redefine the method. The other child
classes can use the parent class method. Due to this, we don’t need to
modification the parent class code

 In polymorphism, Python first checks the object’s class type and executes
the appropriate method when we call the method.

Let’s see how it works with the help of an example.

In this example, we have a vehicle class as a parent and a ‘Car’ and ‘Truck’
as its sub-class. But each vehicle can have a different seating capacity, speed, etc.,
so we can have the same instance method name in each class but with a different
implementation. Using this code can be extended and easily maintained over
time.

Mr. Dhanraj Suresh Kiwde Page 3


class Vehicle:

def __init__(self, name, color, price):

self.name = name

self.color = color

self.price = price

def show(self):

print('Details:', self.name, self.color, self.price)

def max_speed(self):

print('Vehicle max speed is 150')

def change_gear(self):

print('Vehicle change 6 gear')

# inherit from vehicle class

class Car(Vehicle):

def max_speed(self):

print('Car max speed is 240')

Mr. Dhanraj Suresh Kiwde Page 4


def change_gear(self):

print('Car change 7 gear')

# Car Object

car = Car('Car x1', 'Red', 20000)

car.show()

# calls methods from Car class

car.max_speed()

car.change_gear()

# Vehicle Object

vehicle = Vehicle('Truck x1', 'white', 75000)

vehicle.show()

# calls method from a Vehicle class

vehicle.max_speed()

vehicle.change_gear()

Output:

Details: Car x1 Red 20000

Car max speed is 240

Car change 7 gear

Details: Truck x1 white 75000

Vehicle max speed is 150

Vehicle change 6 gear

3. Polymorphism In Class methods

 Polymorphism with class methods is useful when we group different


objects having the same method.

Mr. Dhanraj Suresh Kiwde Page 5


Python will check object type at runtime and call the correct method.
Example
class Ferrari:

def fuel_type(self):

print("Petrol")

def max_speed(self):

print("Max speed 350")

class BMW:

def fuel_type(self):

print("Diesel")

def max_speed(self):

print("Max speed is 240")

ferrari = Ferrari()

bmw = BMW()

# iterate objects of same type

for car in (ferrari, bmw):

# call methods without checking class of object

car.fuel_type()

car.max_speed()

Output

Petrol

Max speed 350

Diesel

Max speed is 240

Mr. Dhanraj Suresh Kiwde Page 6

You might also like