Polymorphism is the ability of objects to take on multiple forms or perform different operations based on the context in which they are used. In Python, polymorphism is achieved through the use of method overriding and method overloading.
Method overriding is a mechanism in which a subclass provides a different implementation of a method that is already defined in its superclass. This allows the subclass to inherit the method from its superclass but also gives it the ability to customize the method's behavior.
Method overloading is a mechanism in which a class can define multiple methods with the same name but with different parameters. When the method is called, the appropriate version of the method is selected based on the number and types of arguments passed to it. Here is an example of polymorphism in Python using method overriding:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
class Cat(Animal):
def speak(self):
print("Cat meows")
# Create instances of each class
animal = Animal()
dog = Dog()
cat = Cat()
# Call the speak method on each instance
animal.speak() # Output: "Animal speaks"
dog.speak() # Output: "Dog barks"
cat.speak() # Output: "Cat meows"
Polymorphism is an extremely important concept in programming, wherein a single entity is used to represent different types in different situations. In Python, these entities can be operators, methods, or classes.
1. Polymorphism of '+' operator:
The '+' operator is used to perform arithmetic addition operations for integer data types.
a = 2
b = 3
print(a+b)
# output = 3
For string data types, the same '+' operator is used to perform concatenation.
Some functions in Python are compatible with multiple data types. An example of such a function is the len() function.
print(len("MindStick") # len() on string
print(len(["Delhi", "Mumbai", "Chennai", "Kolkata"])) # len() on list
print(len({"Name": "John", "Age": 25, "Location": "London"})) # len() on dictionary
# output = 9
4
3
3. Polymorphism of classes:
We can use polymorphism when creating class methods since Python allows different classes to have methods with the same name. We can call these methods without specifying the object as shown:
class Class1:
def __init__(self,name): # constructor
self.name = name
def info(self):
print("This is Class1 with name",self.name)
class Class2:
def __init__(self,name): # constructor
self.name = name
def info(self):
print("This is Class2 with name",self.name)
c1 = Class1("John")
c2 = Class2("Evan")
for c in (c1, c2): # iterate through the tuple using common variable 'c'
c.info()
# output = This is Class1 with name John
This is Class2 with name Evan
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Polymorphism is the ability of objects to take on multiple forms or perform different operations based on the context in which they are used. In Python, polymorphism is achieved through the use of method overriding and method overloading.
Method overriding is a mechanism in which a subclass provides a different implementation of a method that is already defined in its superclass. This allows the subclass to inherit the method from its superclass but also gives it the ability to customize the method's behavior.
Method overloading is a mechanism in which a class can define multiple methods with the same name but with different parameters. When the method is called, the appropriate version of the method is selected based on the number and types of arguments passed to it.
Here is an example of polymorphism in Python using method overriding:
Polymorphism is an extremely important concept in programming, wherein a single entity is used to represent different types in different situations. In Python, these entities can be operators, methods, or classes.
1. Polymorphism of '+' operator:
The '+' operator is used to perform arithmetic addition operations for integer data types.
For string data types, the same '+' operator is used to perform concatenation.
2. Polymorphism of functions:
Some functions in Python are compatible with multiple data types. An example of such a function is the len() function.
3. Polymorphism of classes:
We can use polymorphism when creating class methods since Python allows different classes to have methods with the same name. We can call these methods without specifying the object as shown: