POLYMORPHISM

Polymorphism in Python

What is Polymorphism : 

The word polymorphism means having many forms. In programming, polymorphism means same function name (but different signatures) being uses for different types.

 

Example of inbuilt polymorphic functions :

# Python program to demonstrate in-built poly-

# morphic functions

  

# len() being used for a string

print(len("geeks"))

  

# len() being used for a list

print(len([10, 20, 30]))

 

Output:

5

3

 

Examples of used defined polymorphic functions :

# A simple Python function to demonstrate 

# Polymorphism

  

defadd(x, y, z =0): 

    returnx +y+z

# Driver code 

print(add(2, 3))

print(add(2, 3, 4))

 

Output:

5

9

 

Polymorphism with class methods:
Below code shows how python can use two different class types, in the same way. We create a for loop that iterates through a tuple of objects. Then call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class.

# A simple Python function to demonstrate 

# Polymorphism

  

defadd(x, y, z =0): 

    returnx +y+z

  

# Driver code 

print(add(2, 3))

print(add(2, 3, 4))

 

Output:

5

9

Polymorphism with class methods:
Below code shows how python can use two different class types, in the same way. We create a for loop that iterates through a tuple of objects. Then call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class.

# A simple Python function to demonstrate 

# Polymorphism

  

defadd(x, y, z =0): 

    returnx +y+z

  

# Driver code 

print(add(2, 3))

print(add(2, 3, 4))

 

Output:

5

9

 

Polymorphism with class methods:
Below code shows how python can use two different class types, in the same way. We create a for loop that iterates through a tuple of objects. Then call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class.

deffunc(obj):

    obj.capital()

    obj.language()

    obj.type()

   

obj_ind =India()

obj_usa =USA()

   

func(obj_ind)

func(obj_usa)

 
Code : Implementing Polymorphism with a Function

classIndia():

    defcapital(self):

        print("New Delhi is the capital of India.")

   

    deflanguage(self):

        print("Hindi is the most widely spoken language of India.")

   

    deftype(self):

        print("India is a developing country.")

   

classUSA():

    defcapital(self):

        print("Washington, D.C. is the capital of USA.")

   

    deflanguage(self):

        print("English is the primary language of USA.")

   

    deftype(self):

        print("USA is a developed country.")

  

 

deffunc(obj):

    obj.capital()

    obj.language()

    obj.type()

   

obj_ind =India()

obj_usa =USA()

   

func(obj_ind)

func(obj_usa)

 

Output:

New Delhi is the capital of India.

Hindi is the most widely spoken language of India.

India is a developing country.

Washington, D.C. is the capital of USA.

English is the primary language of USA.

USA is a developed country.

 


Comments