Polymorphism :

In this tutorial we will study polymorphism . Polymorphism names are taken from geeks word ‘Poly’ means “Many” and ‘morphism’ means “Forms”. In programming terms it refers to the particular single method that’s used in many forms.

In Python, language there are many ways to define polymorphism . let’s see the demonstration to understand better how to implement the polymorphism

Here you can see that, Class Kerala and Uttrakhand define, both classes obtaining the same method name ‘language’ and ‘whether’. At last the ‘func’ method defines and it takes one argument ‘obj’ , after that making the object of Kerala and Uttrakhand class every corresponding object passes from ‘func’ as an argument

Program

class Kerala:
    def language(self):
        print("Malayalam")
    def whether(self):
        print("Hot")
class Uttarakhand:
    def language(self):
        print("Hindi")
    def whether(self):
        print("Cool")

def func(obj):

    obj.language()    
    obj.whether()


State=Kerala()
State1=Uttarakhand()

func(State)
func(State1)

Output

Malayalam
Hot
Hindi
Cool

Program

class Kerala:
    def language(self):
        print("Malayalam")
    def whether(self):
        print("Hot")
class Uttrakhand:
    def language(self):
        print("Hindi")
    def whether(self):
        print("Cool")


obj_kerala=Kerala()
obj_Uk=Uttrakhand()

for state in (obj_kerala,obj_Uk):
    state.language()
    state.whether()

Output

Malayalam
Hot
Hindi
Cool
Subscribe Now