Welcome to codeswithpankaj.com! In this tutorial, we will explore the concept of polymorphism in Python. We'll cover what polymorphism is, how it works in Python, and provide detailed examples to illustrate its application.
- Introduction to Polymorphism
- Types of Polymorphism
- Compile-time Polymorphism
- Runtime Polymorphism
- Polymorphism in Python
- Method Overloading
- Method Overriding
- Operator Overloading
- Polymorphism with Inheritance
- Duck Typing
- Practical Examples
- Summary
Polymorphism is a core concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to be used for different data types.
- Flexibility: Polymorphism allows for flexibility and the reuse of code.
- Maintainability: It makes the code easier to maintain and extend.
- Readability: It enhances code readability and organization.
Also known as static polymorphism, it is achieved through method overloading and operator overloading. This type of polymorphism is resolved during compile time.
Also known as dynamic polymorphism, it is achieved through method overriding. This type of polymorphism is resolved during runtime.
Python supports polymorphism through method overriding, operator overloading, and duck typing.
Method overloading allows a class to have multiple methods with the same name but different parameters. Python does not support method overloading by default, but we can achieve it using default arguments.
class MathOperations:
def add(self, a, b, c=0):
return a + b + c
math_op = MathOperations()
print(math_op.add(2, 3))
print(math_op.add(2, 3, 4))Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class.
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
dog = Dog()
dog.sound()Operator overloading allows us to define custom behavior for operators in user-defined classes.
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 3)
v2 = Vector(3, 4)
v3 = v1 + v2
print(v3)Polymorphism works well with inheritance, allowing methods to be overridden in derived classes.
class Bird:
def fly(self):
print("Bird can fly")
class Sparrow(Bird):
def fly(self):
print("Sparrow can fly")
class Ostrich(Bird):
def fly(self):
print("Ostrich cannot fly")
def make_bird_fly(bird):
bird.fly()
sparrow = Sparrow()
ostrich = Ostrich()
make_bird_fly(sparrow)
make_bird_fly(ostrich)Duck typing is a concept where the type or class of an object is less important than the methods it defines. If an object implements certain methods, it can be used in place of another object with similar methods.
class Bird:
def fly(self):
print("Bird can fly")
class Airplane:
def fly(self):
print("Airplane can fly")
def make_it_fly(obj):
obj.fly()
bird = Bird()
airplane = Airplane()
make_it_fly(bird)
make_it_fly(airplane)class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
def print_area(shape):
print(shape.area())
circle = Circle(5)
square = Square(4)
print_area(circle)
print_area(square)print(len("codeswithpankaj.com")) # String length
print(len([1, 2, 3, 4, 5])) # List length
print(len({"a": 1, "b": 2, "c": 3})) # Dictionary lengthIn this tutorial, we explored the concept of polymorphism in Python, its types, and how to implement it using method overloading, method overriding, operator overloading, and duck typing. We also provided practical examples to illustrate the application of polymorphism in Python. Polymorphism is a powerful feature that enhances code flexibility, readability, and maintainability.
For more tutorials and in-depth explanations, visit codeswithpankaj.com!
This tutorial provides a comprehensive overview of Python polymorphism, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!