# Difference Between Inheritance and Polymorphism

In computer programming, Inheritance and Polymorphism are two important concepts. The most basic difference between inheritance and polymorphism is that "inheritance" is a concept of objectoriented programming that allows creating a new class with the help of the features of an existing class, whereas the concept "polymorphism" represents multiple forms of a single function.

## What is Inheritance?

Inheritance is a concept in object-oriented programming (OOP) that refers to the process by which an object can take on the features of one or more other objects. Therefore, inheritance is one of the most powerful concepts for establishing code reusability in OOPs.

With the help of inheritance, we can build a new class by creating parent-child connections with already existing classes. Hence, inheritance allows for the creation of new subtypes of pre-existing classes.

### Example

In OOPs, the primary objective of using inheritance is to reduce the length of code through the concept of reusability. There are many types of inheritance, namely single inheritance, multiple inheritance, hierarchical inheritance, multilevel inheritance, and hybrid inheritance.

class base_class:
def __init__(self, f_name, l_name):
self.firstname = f_name
self.lastname = l_name
def print_it(self):
print(self.firstname, self.lastname)
print("An instance of 'base_class' is created")
my_instance = base_class("John", "Will")
print("A method 'print_it' is being called using the created instance")
my_instance.print_it()


### Output

It will produce the following output

An instance of 'base_class' is created
A method 'print_it' is being called using the created instance
John Will


## What is Polymorphism?

Polymorphism means multiple forms. It is a concept in programming that allows a single function to have multiple forms. Polymorphism is generally implemented on functions and it allows an object to determine which form of the function is to be invoked.

There are two types of polymorphism namely, compile-time polymorphism and runtime polymorphism. Compile-time polymorphism is also known as method overloading, while runtime polymorphism is also known as method overriding.

### Example

def add_vals(val_1, val_2, val_3 = 1):
my_result = val_1 + val_2 + val_3
return my_result

print("The method is being called by passing two parameters")
print("The method is being called by passing three parameters")


### Output

It will produce the following output

The method is being called by passing two parameters
17
The method is being called by passing three parameters
79


## Difference between Inheritance and Polymorphism

The following are the important differences between Inheritance and Polymorphism −

S.No.

Inheritance

Polymorphism

1.

It is a part of object-oriented programming paradigm.

'Poly' means multiple and 'morph' means forms.

2.

It can be implemented in C++, Java, Python, and other object-oriented programming languages.

It is a part of object-oriented programming paradigm.

3.

It is the method in which a new class is created that can take and use the properties of an already existing class.

It can be implemented in C++, Java, Python, and other object-oriented programming languages.

4.

The already existing class is known as 'parent/base class' and the class that uses this class is known as 'child/derived class'.

Hence, polymorphism refers to the method of performing a specific task in multiple ways.

5.

It helps reuse the code.

This can be used while using functions.

6.

It reduces the size of the code while implementing object oriented programming.

It allows the object of the class to decide which form it has to take to work with methods and attributes of the class.

7.

Types of inheritance

• Single inheritance

• Multi-level inheritance

• Multiple inheritance

• Hybrid inheritance

• Hierarchical inheritance

Types of polymorphism

• Run-time polymorphism- It is also known as method overriding.

8.

It can be used in pattern design.

It can be used in pattern design.

## Conclusion

The most significant difference between the two concepts is that inheritance is used to increase the concept of reusability to decrease the code length in the program, while polymorphism defines the concept by which a single function can be invoked in multiple forms.