Polymorphism in Python

  

Concept


Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Polymorphism in python defines methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. Also, it is possible to modify a method in a child class that it has inherited from the parent class



Code Example


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


First Let me show you Builtin Example:

len('Hello world')

len([1,2,3,4,5])

len((1,2,3,4,5))


From the above example, you can see len() function is taking different from of input. 


def add(x=1, y=2, z=3):

      res = x + y + z

      return res


print(add()) ---> 6

print(add(5,2))  ----> 10

print(add(1,1,1))  ----> 3


# OOP Concept

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()




Quiz Level 1

Quiz Level 2


Request Me


Click Here For More





Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python