Polymorphism
Polymorphism refers to having several different forms . It allows programmers to assign different meanings to a variable ,objects or method with the same name . Polymorphism is one of the important features of OOP. In python Polymorphism is simply means defining a number of subclass that have methods of the same name .
Types Of Polymorphism :
In Python there are different ways to provide Polymorphism like Method Overloading and Operator Overloading also Method Overriding .
Method Overloading :
Method Overloading means having a method of same name but different numbers
arguments. In other programming language like JAVA it support Method overloading
directly . and compiler overload Method in which number of argument is same as passed
arguments .But python doesn't support Method overloading directly . so to use Method
overloading in python we have to use a concept called as default argument
EXAMPLE :
In above example we can pass different number of argument for same method and getting
expected Output .in this way we can implement Method overloading in Python .
Method Overriding :
Method overriding is Nothing but having two methods with same name and same number
of argument . A same class cannot have two methods of same name and same number of
that .
Example :
In above example we have two classes in which greet inherits greeting .in both class we
have method show but compiler is calling subclass method this what method overriding
means. method of superclass is overridden by subclass method . but if we don't have that
method in subclass . then the compiler will print super class method .
Operator Overloading :
Like Method overloading operator overloading also a form of compile time polymorphism
In operator overloading different operators have different implementation depending on their
argument .with operator overloading a programmer is allow to provide his own definition for
an operator by overloading Built in data type .
Example :
Program to overload + operator on a complex object .
In above program we use " + " operator to add obj1 and obj 2, we had define __add__()
Method which overload the inbuilt add operator .
We can also overload other operators but for that we must know function name for that
Operator which are given below