Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Presentation

polymorphism in python

Rating
-
Sold
-
Pages
13
Uploaded on
25-04-2024
Written in
2023/2024

describes about the polymorphism in python and the various types used in polymorphism

Institution
Course

Content preview

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

Written for

Course

Document information

Uploaded on
April 25, 2024
Number of pages
13
Written in
2023/2024
Type
PRESENTATION
Person
Unknown

Subjects

$9.29
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
rpjpraveen77

Get to know the seller

Seller avatar
rpjpraveen77 Achyuta academy
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
6
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions