Polymorphism in Python
Polymorphism is a core concept in object-oriented programming (OOP) that means “many forms.” In Python, polymorphism allows different classes to be treated as instances of the same class through shared methods or interfaces. This allows for writing more flexible and reusable code.
Why Use Polymorphism?
● Flexibility: It allows us to define a common interface for different types of objects.
● Reusability: Functions and methods can work on objects of different classes that share the same interface,
making them reusable and extensible.
Method Overriding
Method overriding is a feature in object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. When a method in a subclass has the same name and parameters as a method in its superclass, the subclass’s method overrides the one in the superclass.
In Python, method overriding happens automatically when a subclass defines a method with the same name as one in its superclass.
Example:
In this program, we will create an Animal class with a method make_sound() that describes a generic animal sound. Then, we will create a Dog class that inherits from Animal and overrides the make_sound() method to provide a specific sound for dogs.
# Define a base class Animal
class Animal:
# Define a method to make a generic animal sound
def make_sound(self):
print(“The animal makes a generic sound.”)
def sleep(self):
print(“Animal is sleeping.”)
# Define a Dog class that inherits from Animal
class Dog(Animal):
# Override the make_sound method to specify a dog’s sound
def make_sound(self):
print(“The dog barks: Woof! Woof!”)
# Create a Dog object and call the make_sound method
my_dog = Dog() # Create an instance of Dog
my_dog.make_sound() # Call the overridden method
my_dog.sleep()
#Output: The dog barks: Woof! Woof!
#Output: Animal is sleeping
Course Video
Course Video English:
Tasks:
1. Basic Overriding:
Write a base class Shape with a virtual method Draw(). Create a derived class Circle that overrides the Draw() method to print “Drawing Circle”.
2. Polymorphism with Overriding:
Define a base class Animal with a virtual method MakeSound(). Create derived classes Dog and Cat that override MakeSound() to print “Bark” and “Meow” respectively. Demonstrate polymorphism by calling MakeSound() on a list of Animal objects.
3. Calling Base Class Method:
Create a base class Vehicle with a virtual method Describe(). In a derived class Car, override Describe() but also call the base class Describe() method within the overridden method.
4. Abstract Methods:
Write an abstract class Employee with an abstract method CalculateSalary(). Create derived classes FullTimeEmployee and PartTimeEmployee that implement the CalculateSalary() method.
5. Overriding Properties:
Define a base class Person with a virtual property Name. Create a derived class Student that overrides the Name property to add custom behavior when setting the property value.
Task Video
YouTube Reference :
It allows different objects to respond to the same method call in different ways.
Same method, different behaviors depending on the class (e.g., sound() in Dog vs Cat).
Static: Resolved at compile time. Dynamic: Resolved at runtime.
Object-Oriented Programming, involving classes and objects.
Ability to call the same method on different objects and get different results.
Enhances flexibility and code reusability.
It allows different objects to respond to the same method call in different ways.
Overloading, Overriding, Compile-time, and Runtime.
Polymorphism allows different classes to define the same method, but with different implementations.
Polymorphism allows one interface to be used for different data types. For example, the __str__() method can be implemented differently in various classes, allowing objects of those classes to print in different formats.