Polymorphism in Python



Polymorphism

The term polymorphism has been derived from the Greek words 'poly' and 'morphos' which means many and forms respectively. By polymorphism, we understand that one task can be performed in different ways.
        We have already seen that len() function operates on various types of objects such as list, str, tuples, set etc.

For example:

print(len("Engine4geeks"))  #len here is used for string

print(len([12,23,3,43]))   #len here is used for list

Example 

class India(): 
def capital(self): 
print("New Delhi is the capital of India.") 

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

def type(self): 
print("India is a developing country.") 

class USA(): 
def capital(self): 
print("Washington, D.C. is the capital of USA.") 

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

def type(self): 
print("USA is a developed country.") 

obj_ind = India() 
obj_usa = USA() 
for country in (obj_ind, obj_usa): 
country.capital() 
country.language() 
country.type() 


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.





 

 

Post a Comment

0 Comments