Polymorphism in OOPs

Polymorphism in Python is allows to make different form same method name but different parameter.It is two divided into two part method overriding method overloading.

Polymorphism in Python

Method Overloading

In method overloading, class has two or more methods with same name but with different parameter list is known as method overloading.it is the signature of methods must be different but the return type can be same or different.

Syntax
class mainclass:
   def main1(parameter1,parameter2):
     #statement 
   def main1(parameter2,parameter1):
     #statement
  -----------------------------or--------------------------------------
class mainclass:
   def main1(parameter1,parameter2):
     #statement 
   def main1(parameter3,parameter4)
     #statement 
Note

If the parameter same name but different datatype,then both parameter are different.

Example
class parent:
   def parentclass(self,x,y,z):
     print("Sum1=",self.x+self.y+self.y)
class child(parent):
   def parentclass(self,x,y):
     print("sum2=",x+y)
obj1=parent()
obj1.parentclass(4,8,13)

Output

Vikas Chaurasiya

Method Overriding

In medod overriding,declaration of a method in sub class which is already present in parent class is known as method overriding. Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method.

Syntax
class mainclass:
   def main1(parameter1,parameter2):
      #statement 
   def main1(parameter2,parameter1):
      #statement
Example
class parent:
   def parentclass(self):
      print("This is Parent1 Class")
class child(parent):
   def child1class(self):
     print("This is child1 Class")
class child1(child):
   def child1class(self):
     print("This is child1 Class")
class child2(child):
   def child2class(self):
     print("This is child2 Class")
obj1=child1()
obj1.parentclass()
obj2.parentclass()

Output

This is Parent1 Class
This is Parent1 Class