Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Inheritance?
2.1.
Inheritance Example
2.2.
Benefits of Inheritance 
3.
What is Polymorphism?
3.1.
Polymorphism Example
3.2.
Benefits of Polymorphism 
4.
Difference Between Inheritance and Polymorphism
5.
Frequently Asked Questions
5.1.
What is OOPs?
5.2.
What is the difference between a class and a structure?
5.3.
What are the advantages of Polymorphism?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Difference Between Inheritance and Polymorphism

Author Nitika
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In object-oriented programming, Inheritance and Polymorphism hold a significant value. In this blog, we will be studying the difference between inheritance and Polymorphism with some examples. So buckle up and enjoy the journey.

Difference Between Inheritance and Polymorphism

What is Inheritance?

Inheritance is a mechanism where an object acquires all the properties of the parent object. On creating a new class, the former inherits all the properties of the exit class. This process is done without changing the properties of the existing exit class. It may add some fresh features. The new class is called the Derived Class or Child Class, and the existing one is the Base Class or Parent Class.

Sub-Class is a class which inherits the properties from another class.

Super Class is the class whose features are inherited by a Subclass.

There are five types of inheritance-

  1. Single inheritance: This type allows a class to inherit properties from one class only.
     
  2. Multilevel inheritance In this type, a derived class is constructed from another derived class.
     
  3. Multiple inheritance: A class can inherit from various classes of this type.
     
  4. Hybrid (Virtual) inheritance: This type of inheritance includes merging more than one type of inheritance.
     
  5. Hierarchical inheritance: This type of inheritance allows more than one subclass to be inherited by one base class.

Also see,  Swap Function in Java

Inheritance Example

Let us understand further with some examples:

#include<iostream>
using namespace std;

class A {
    int num1, num2;

public:
    void add() {
        cout << "Enter two numbers: ";
        cin >> num1 >> num2;
        cout << (num1 + num2) << endl;
    }
};

class B : public A {
public:
    void print() {
        add();
    }
};

int main() {
    B p1;
    p1.print();
    return 0;
}

 

Output

Enter Two Numbers: 4 5
9

 

Also see, Duck Number in Java and Hashcode Method in Java

Benefits of Inheritance 

  • Less development and maintenance costs are required as the existing code is reusable.
     
  • Inheritance enables reusability. When a class inherits another class, it is accessible to all the functionality of the inherited class.
     
  • In Inheritance, reusability enhances reliability.
     
  • Inheritance allows the reduction of redundancy in codes and supports the extensibility of the code.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Polymorphism?

The word Polymorphism means having many forms. Polymorphism is considered one of the essential elements of Object-Oriented Programming. It lets the object determine which form of the function to implement at compile and at run-time.

There are two types of Polymorphism-

  1. Compile-time Polymorphism (Method Overloading): This type is achieved by function or operator overloading.
     
  2. Runtime Polymorphism (Method Overriding): This type of Polymorphism is achieved by Function Overriding. 

Polymorphism Example

Let us understand further with some examples:

#include <iostream>
using namespace std;
class A {
    int a, b, c;
    
public:

    void add() {
        cout << "Enter two numbers: ";
        cin >> a >> b;
        cout << "Sum: " << (a + b) << endl;
    }
    
    void add_three() {
        cout << "Enter three numbers: ";
        cin >> a >> b >> c;
        cout << "Sum: " << (a + b + c) << endl;
    }
    
    virtual void print() {
        cout << "Class A" << endl;
    }
};

class B : public A {

public:
    void print() {
        cout << "Class B" << endl;
    }
};

int main() {
    A a1;
    B b1;

    a1.add();
      a1.add_three();

    b1.print();
    return 0;
}

 

Output

Enter two numbers: 5 6
Sum: 11
Enter three numbers: 2 3 4
Sum: 9
Class B

Benefits of Polymorphism 

  • The codes can be reused with the help of Polymorphism.
     
  • It saves a lot of time since classes are written, tested and implemented at once. 
     
  • Polymorphism offers flexibility to programmers to write programs. It uses one method for multiple executions as needed. 
     
  • Polymorphism supports singular variable names for multiple data types.
     
  • Coupling between various functionalities is reduced using Polymorphism.

Difference Between Inheritance and Polymorphism

We will now explore the difference between inheritance and Polymorphism.
 

Point of difference

Inheritance 

Polymorphism

Definition

Inheritance is a mechanism where an object acquires all the properties of the parent object.

Polymorphism is that which can be defined in multiple forms.

Implementation

Inheritance is applied to the classes.

Polymorphism is applied to methods or functions.

Use

Reusability and reduction of code length are seen in Inheritance.

In Polymorphism, the declared object makes the decision of deciding which form of the function is to be called. 

Forms

Inheritance has five types:

 

  • Single inheritance
  • Multi-level inheritance
  • Multiple inheritance
  • Hybrid inheritance
  • Hierarchical inheritance

 

Polymorphism has two types:

 

  • Compile-time Polymorphism (Method Overloading)
  • Runtime Polymorphism (Method Overriding)

 

Examples

The class 'car' can inherit the feature of the class 'vehicle'.

The class 'car' can also have the function 'set_color()' and a class 'truck' can also have the function 'set_color()'

Frequently Asked Questions

What is OOPs?

Object-Oriented Programming(OOPs) is a form of the journey of programming which is based on objects instead of just functions and procedures. Singular objects are grouped into classes. OOPs allow clarity in programming, thereby allowing simplicity in solving complex problems.

What is the difference between a class and a structure?

A Class is a user-defined blueprint from which objects are formed. It consists of a set of instructions that are to be completed on the objects. In comparison, a structure is a  user-defined pack of variables which are of a distinct data type.

What are the advantages of Polymorphism?

By using polymorphism, code flexibility is achieved as various operations by using methods with identical names according to the needs can be performed. The main advantage of using polymorphism is implementation to an abstract base class or an interface can be utilised.

Conclusion

In this article, we explored the depths of the difference between Inheritance and Polymorphism. If you wish to know more about Polymorphism, do check out Virtual Functions & Runtime Polymorphism in C++ and Inheritance in Java .

Recommended Read:

Yarn vs NPM

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSQLSystem Design, and many more!
If you want to test your competency in coding, you may check out the Mock Test Series and participate in the Contests organised on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the ProblemsInterview Experiences, and Interview Bundle for placement preparations.

Nevertheless, you may consider our Courses to give your career an edge over others!

Happy Learning Ninja!!  

Previous article
Single Inheritance in Java
Next article
Multilevel Inheritance in Java
Live masterclass