Polymorphism comprises of two words poly+morphs.
Poly means many and morphs means forms.
If a single entity has different behaviour in different conditions then this condition is called polymorphism.
Polmorphism can be implemented using two ways.
Example of function overriding
class person:
def __init__(self,name):
self.name=name
def viewProperties(self):
print("You are in general category")
print("Your name is ",self.name)
class student(person):
def __init__(self,name):
super().__init__(name)
def viewProperties(self):
print("Your category is student")
print("Your name is",self.name)
p1=person("Aniket")
p1.viewProperties()
s1=student("Mayank")
s1.viewProperties()
Output:
You are in general category
Your name is Aniket
Your category is student
Your name is Mayank
Note: In other object oriented languages like JAVA and C++, function overloading is possible.
Poly means many and morphs means forms.
If a single entity has different behaviour in different conditions then this condition is called polymorphism.
Polmorphism can be implemented using two ways.
- Function overloading
- Function overriding
Function Overloading and overriding:
Function overloading basically means functions with same names but different arguments.
But function overloading is not possible in Python language. In Python, if we are declaring more than one function with the same name, then the function that we declare currently will override the previous declared function. So function overloading is not possible in Python.
Below script will give you error due to function overriding.
Example of function overloading
class A:
def f1(self):
print("This is the function of class A")
class B(A):
def f1(self,a):
print("This is the function of class B")
a1=A()
a1.f1()
b1=B()
b1.f1()
Output:
This is the function of class A
Traceback (most recent call last):
File "C:\Users\hp\Desktop\p1.py", line 12, in <module>
b1.f1()
TypeError: f1() missing 1 required positional argument: 'a'
I think you got why the error is coming.
But function overloading is not possible in Python language. In Python, if we are declaring more than one function with the same name, then the function that we declare currently will override the previous declared function. So function overloading is not possible in Python.
Below script will give you error due to function overriding.
Example of function overloading
class A:
def f1(self):
print("This is the function of class A")
class B(A):
def f1(self,a):
print("This is the function of class B")
a1=A()
a1.f1()
b1=B()
b1.f1()
Output:
This is the function of class A
Traceback (most recent call last):
File "C:\Users\hp\Desktop\p1.py", line 12, in <module>
b1.f1()
TypeError: f1() missing 1 required positional argument: 'a'
I think you got why the error is coming.
Example of function overriding
class person:
def __init__(self,name):
self.name=name
def viewProperties(self):
print("You are in general category")
print("Your name is ",self.name)
class student(person):
def __init__(self,name):
super().__init__(name)
def viewProperties(self):
print("Your category is student")
print("Your name is",self.name)
p1=person("Aniket")
p1.viewProperties()
s1=student("Mayank")
s1.viewProperties()
Output:
You are in general category
Your name is Aniket
Your category is student
Your name is Mayank
Note: In other object oriented languages like JAVA and C++, function overloading is possible.
Be First to Post Comment !
Post a Comment