Polymorphism
Introduction to OOP's
Class
Object
Constructor
Instance Variables
Class Variables
Instance Methods
self Keyword
Encapsulation
Public Members
Protected Members
Private Members
Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
super() Function
Polymorphism
Method Overriding
Duck Typing
Abstraction
Abstract Class
Abstract Method
Class Method
Static Method
Special Methods
_str_ Method
Operator Overloading
Composition
Aggregation
Association
Method Resolution Order
Object Class
issubclass() and isinstance()
Property Decorator
Setter Method with @property
Destructor
Inner Class
Complete Mini Example
Polymorphism
19. Polymorphism
Polymorphism allows the same method name or interface to behave differently for different objects. In Python, this is commonly shown through method overriding and duck typing.
Syntax
class A:
def show(self):
pass
class B(A):
def show(self):
print("Different behavior")
Example
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
for animal in [Cat(), Dog()]:
animal.sound()
Output
Meow
Bark
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Polymorphism to test your knowledge.
Browse Quizzes »