Polymorphism in Python
Object-Oriented Programming (OOP) has four essential characteristics: abstraction,
encapsulation, inheritance, and polymorphism.
What is 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.
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.
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
3
10
Polymorphism With Inheritance
Polymorphism is mainly used with inheritance. In inheritance, child class inherits the attributes
and methods of a parent class. 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.
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. 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
Object-Oriented Programming (OOP) has four essential characteristics: abstraction,
encapsulation, inheritance, and polymorphism.
What is 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.
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.
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
3
10
Polymorphism With Inheritance
Polymorphism is mainly used with inheritance. In inheritance, child class inherits the attributes
and methods of a parent class. 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.
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. 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