POLYMORPHISM
polymorphism is an important feature of class definition in Python that is utilized when you have commonly named methods across classes or sub classes. Polymorphism can be carried out through inheritance, with sub classes making use of base class methods or overriding them.
There are two types of polymorphism:
1.Overloading
2.Overriding
Overloading:
Overloading occurs when two or more methods in one class have the same method name but different parameters.
Overloading is divided into two types
.Operator Overloading
. Method Overloading
Operator overloading: Operator overloading is the type of overloading in which an operator can be used in multiple ways beyond its predefined meaning.
Method Overloading: Method overloading means a class containing multiple methods with the same name but may have different arguments. Basically python does not support method overloading, but there are several ways to achieve method overloading. Though method overloading can be achieved, the last defined methods can only be usable.
Polymorphism with Function and Objects:
You can create a function that can take any object, allowing for polymorphism.
Let’s take an example and create a function called “func()” which will take an object which we will name “obj”. Now, let’s give the function something to do that uses the ‘obj’ object we passed to it. In this case, let’s call the methods type() and color(), each of which is defined in the two classes ‘Tomato’ and ‘Apple’. Now, you have to create instantiations of both the ‘Carrot’ and ‘Apple’ classes if we don’t have them already:
class carrot():
def
type(self):
print("Vegetable")
def
color(self):
print("orange")
class
Apple():
def
type(self):
print("Fruit")
def
color(self):
print("Red")
def
func(obj):
obj.type()
obj.color()
obj_carrot = carrot()
obj_apple =
Apple()
func(obj_tomato)
func(obj_apple)
Output:
Vegetable
Orange
Fruit
Red
Polymorphism with Class Methods
Python uses two different class types in the same way. Here, you have to create a for loop that iterates through a tuple of objects. Next, you have to call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class. Example for class method:
class Car():
def wheels(self):
print(3)
def mode_of_transport(self):
print(“Public”)
class Bus():
def wheels(self):
print(9)
def mode_of_transport(self):
print(“Private”)
obj_car = Car()
obj_bus = Bus()
for vehicle in (obj_car, obj_bus):
vehicle.wheels()
vehicle.mode_of_transport()
Output
3
public
9
private
Inheritance
Before we get into method overriding, it’s necessary to understand Python’s initial inheritance. Inheritance is the method by which a class may be derived from any base class, with the derived class inheriting all of the base class’s attributes. Inheritance alleviates the challenge of repeatedly writing the same code and enhances reusability.
Example of inheritance:
class Bird://base class Bird
def sound(self):
print("Birds Sounds")
#сhild сlаss Dоg inherits the bаse сlаss Аnimаl
class Sparrow(Bird)://child class Sparrow
def tweet(self):
print("sparrow tweeting")
d = Sparrow()
d.tweet()
d.sound()
Output:
Sparrow tweeting
Birds Sound
Method Overriding:
Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.
There are different methods to use polymorphism in Python. You can use different function, class methods or objects to define the polymorphism.
class Bird:
def intro(self):
print(“There are many types of birds.”)
def flight(self):
print(“Most of the birds can fly but some cannot.”)
class sparrow(Bird):
def flight(self):
print(“Sparrows can fly.”)
class ostrich(Bird):
def flight(self):
print(“Ostriches cannot fly.”)
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
Output:
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.