> For the complete documentation index, see [llms.txt](https://gyansetu-python.gitbook.io/python-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-python.gitbook.io/python-programming/oops/polymorphism-data-hiding.md).

# Polymorphism

**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.

&#x20;**Example of inbuilt polymorphic functions :**

```python
# 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])) 
```

5

3

&#x20;**Examples of user defined polymorphic functions :**

```python
# A simple Python function to demonstrate  
# Polymorphism 
  
def add(x, y, z = 0):  
    return x + y+z 
  
# Driver code  
print(add(2, 3)) 
print(add(2, 3, 4))
```

5

9

&#x20;**Polymorphism with class methods:**

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

New Delhi is the capital of India.&#x20;

Hindi is the most widely spoken language of India.&#x20;

India is a developing country.&#x20;

Washington, D.C. is the capital of USA.&#x20;

English is the primary language of USA.&#x20;

USA is a developed country.

&#x20;**Polymorphism with Inheritance:**

```python
class Bird: 
  def intro(self): 
    print("There are many types of birds.") 
      
  def flight(self): 
    print("Most of the birds can fly but some cannot.") 
    
class sparrow(Bird): 
  def flight(self): 
    print("Sparrows can fly.") 
      
class ostrich(Bird): 
  def flight(self): 
    print("Ostriches cannot fly.") 
      
obj_bird = Bird() 
obj_spr = sparrow() 
obj_ost = ostrich() 
  
obj_bird.intro() 
obj_bird.flight() 
  
obj_spr.intro() 
obj_spr.flight() 
  
obj_ost.intro() 
obj_ost.flight() 
```

There are many types of birds.&#x20;

Most of the birds can fly but some cannot.&#x20;

There are many types of birds.&#x20;

Sparrows can fly.&#x20;

There are many types of birds.&#x20;

Ostriches cannot fly.

&#x20;**Polymorphism with a Function and objects:**

```python
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.") 
  
def func(obj): 
    obj.capital() 
    obj.language() 
    obj.type() 
   
obj_ind = India() 
obj_usa = USA() 
   
func(obj_ind) 
func(obj_usa) 
```

New Delhi is the capital of India.&#x20;

Hindi is the most widely spoken language of India.&#x20;

India is a developing country.&#x20;

Washington, D.C. is the capital of USA.&#x20;

English is the primary language of USA.&#x20;

USA is a developed country.
