Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Polymorphism and Inheritance in OOP

Difference Between Polymorphism and Inheritance in OOP

January 16, 2018 Posted by Lithmee

Key Difference – Polymorphism vs Inheritance in OOP
 

Object-Oriented Programming (OOP) is commonly used to develop software. Many programming languages support object-oriented programming. Object-oriented programming is a methodology to design a program using classes and objects. A class in OOP is a blueprint to create an object. A class has properties and methods. An object is an instance of a class. OOP contains four pillars such as Inheritance, Polymorphism, Abstraction and Encapsulation. This article discusses the difference between Polymorphism and Inheritance in OOP. The key difference between Polymorphism and Inheritance in OOP is that Polymorphism is the ability of an object to behave in multiple ways and Inheritance is to create a new class using properties and methods of an existing class.

CONTENTS

1. Overview and Key Difference
2. What is Polymorphism in OOP
3. What is Inheritance in OOP
4. Similarities Between Polymorphism and Inheritance in OOP
5. Side by Side Comparison – Polymorphism vs Inheritance in OOP in Tabular Form
6. Summary

What is Polymorphism in OOP?

Polymorphism is to indicate multiple forms. One object can have multiple behaviours. Polymorphism can be divided into two categories. They are overloading and overriding.

Overloading

Refer the below program written in Java.

Difference Between Polymorphism and Inheritance in OOP

Figure 01: Overloading

According to the above program, an object of type A is created. When calling obj.sum(); it will give the output related to method sum(). When calling the obj.sum(2,3); it will give the output related to sum(int a, int b). It can be observed that the same object has different behaviours depending on the situation. When there are multiple methods with the same name, but with different parameters, it is known as overloading. It is also known as static binding or compile time polymorphism.

Overriding

Another type of Polymorphism is overriding. Refer the below program written in Java.

Difference Between Polymorphism and Inheritance in OOP_Figure 02

Figure 02: Overriding

According to the above program, there is a method display() in class A. Class B extends from class A. Therefore, all methods in class A is accessible by class B. It is inheritance. The inheritance concept is described further later.

Class B also have the same method display(). When creating an object of type A and calling display method, the output will give B. Class A display method is overridden by class B display method. So, the output is B.

When there are methods with same name and same parameters but in two different classes, and they are linked with inheritance it is known as overriding. It is also known as Late binding, Dynamic Binding, Runtime Polymorphism. Overloading and overriding are called as Polymorphism. It is a major concept in Object Oriented Programming.

What is Inheritance in OOP?

Refer the below program written in Java.

Key Difference Between Polymorphism and Inheritance in OOP

Figure 03: Example of Inheritance

According to the above program, class A has method sum() and class B has method sub().

The sum() method of class A can be used in class B using extend keyword. Reusing the properties and methods in an existing class to create a new class is known as Inheritance. Even there is no sum() method in class B; it is inherited from class A. Inheritance is useful for code reusability. The older class is called the base class, superclass or parent class. The derived class is called the subclass or child class.

Types of Inheritance

There are various types of inheritance. They are Single-Level Inheritance, Multi-Level Inheritance, Multiple Inheritance, Hierarchical Inheritance and Hybrid Inheritance.

Single Inheritance

In Single Inheritance, there is a one super class and one sub class. If class A is the super class and class B is the sub class, all the properties and methods of class A is accessible by class B. There is only one level; therefore, it is called as single-level inheritance.

Multi-Level Inheritance

In Multi-Level Inheritance there are three levels of classes. The intermediate class inherits from super class. The sub class inherits from the intermediate class. If there are three classes as A, B and C and A is the super class and B is the intermediate class. Then B inherits from A and C inherits from B, it is a Multi-Level Inheritance.

Multiple Inheritance

In Multiple Inheritance, there are many super classes and one sub class. If there are three super classes called A, B, C and D is the sub class, then class D can inherit from A, B and C. Multiple Inheritance is supported in programming language C++. It is not supported in programming languages such as Java or C#. Interfaces are used for implementing Multiple Inheritance in these languages.

Hierarchical Inheritance

If there are classes called A as super classes and B, C are sub classes, those sub classes can inherit properties and methods of class A. That kind of inheritance type is known as Hierarchical Inheritance.

Hybrid Inheritance

There is another special inheritance type which is known as Hybrid Inheritance. It is a combination of multi-level and multiple inheritances. If A, B, C and D are classes and B is inheriting from A and D is inheriting from both B and C, then it is a Hybrid inheritance.

What are the Similarities Between Polymorphism and Inheritance in OOP?

  • Both are concepts of Object Oriented Programming.

What is the Difference Between Polymorphism and Inheritance in OOP?

Polymorphism vs Inheritance in OOP

Polymorphism is an ability of an object to behave in multiple ways. Inheritance is to create a new class using properties and methods of an existing class.
 Usage
Polymorphism is used for objects to call which form of methods at compile time and runtime. Inheritance is used for code reusability.
Implementation
Polymorphism is implemented in methods. Inheritance is implemented in classes.
 Categories
Polymorphism can be divided into overloading and overriding. Inheritance can be divided into single-level, multi-level, hierarchical, hybrid, and multiple inheritance.

Summary – Polymorphism vs Inheritance in OOP 

Polymorphism and Inheritance are major concepts in Object Oriented Programming. The difference between Polymorphism and Inheritance in OOP is that Polymorphism is a common interface to multiple forms and Inheritance is to create a new class using properties and methods of an existing class. Both concepts are widely used in Software Development.

Download the PDF Polymorphism vs Inheritance in OOP

You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between Polymorphism and Inheritance in OOP

Reference:
  1. 8.3 What is Inheritance in Java | Lecture | Tutorial, Telusko Learning, 15 May 2014. Available here  
  2. Polymorphism in Java Tutorial, Telusko Learning, 15 May 2014. Available here

Related posts:

Difference Between Implements and Extends Difference Between Abstract Class and Inheritance Difference Between Arrays and Linked Lists Difference Between Java and Spring Difference Between Open Source and Proprietary SoftwareDifference Between Open Source and Proprietary Software

Filed Under: Programming Tagged With: Compare Polymorphism and Inheritance in OOP, Hierarchical Inheritance, Hybrid Inheritance, Inheritance in OOP, Inheritance in OOP Categories, Inheritance in OOP Definition, Inheritance in OOP Implementation, Inheritance in OOP Usage, Multi-Level Inheritance, multiple inheritance, Polymorphism and Inheritance in OOP Differences, Polymorphism and Inheritance in OOP Similarities, Polymorphism in OOP, Polymorphism in OOP Categories, Polymorphism in OOP Definition, Polymorphism in OOP Implementation, Polymorphism in OOP Usage, Polymorphism in OOP vs Inheritance in OOP, Single Inheritance, types of inheritance

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

What is the Difference Between Tuberculosis and Pneumonia

What is the Difference Between Tuberculosis and Pneumonia

What is the Difference Between Ortho Pyro and Meta Phosphoric Acid

What is the Difference Between Ortho Pyro and Meta Phosphoric Acid

Difference Between Fish Oil and Cod Liver Oil

Difference Between Determinate and Indeterminate Sentencing

Difference Between Determinate and Indeterminate Sentencing

Difference Between Arginine and L-Arginine

Latest Posts

  • What is the Difference Between Calcitriol and Cholecalciferol
  • What is the Difference Between Protein C and Protein S Deficiency
  • What is the Difference Between Raynaud’s Syndrome and Raynaud’s Phenomenon
  • What is the Difference Between Magnesium Citrate Malate and Glycinate
  • What is the Difference Between Appendicitis and Ovarian Cyst
  • What is the Difference Between PAD and Venous Insufficiency
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.