Do you think IIT Guwahati certified course can help you in your career?
No
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.
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-
Single inheritance: This type allows a class to inherit properties from one class only.
Multilevel inheritance In this type, a derived class is constructed from another derived class.
Multiple inheritance: A class can inherit from various classes of this type.
Hybrid (Virtual) inheritance: This type of inheritance includes merging more than one type of inheritance.
Hierarchical inheritance: This type of inheritance allows more than one subclass to be inherited by one base class.
Key Points About Inheritance
Below are key points about inheritance in object-oriented programming:
Definition: Inheritance allows a class (subclass/derived class) to inherit behaviors and properties from another class (superclass/base class).
Access Modifiers: Determines accessibility of inherited members (public, protected, private).
Code Reusability: Promotes code reuse by inheriting common functionality from base classes.
Overriding: Subclasses can override superclass methods to provide specialized behavior.
Superclass Features: Subclasses can access and extend superclass features.
IS-A Relationship: Describes an "is-a" relationship between subclasses and superclasses.
Polymorphism: Supports polymorphic behavior where superclass references can refer to subclass objects.
Constructors and Destructors: Inherited and called automatically based on inheritance rules.
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.
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-
Compile-time Polymorphism (Method Overloading): This type is achieved by function or operator overloading.
Runtime Polymorphism (Method Overriding): This type of Polymorphism is achieved by Function Overriding.
Key Points About Polymorphism
Here are key points about polymorphism in object-oriented programming:
Definition: Polymorphism allows objects of different classes to be treated as objects of a common superclass.
Types: Includes compile-time (static) polymorphism (method overloading) and runtime (dynamic) polymorphism (method overriding).
Method Overloading: Enables a class to have multiple methods with the same name but different parameters.
Method Overriding: Subclasses can provide a specific implementation of a method that is already defined in its superclass.
Dynamic Binding: The actual method implementation called is determined at runtime based on the object type.
IS-A Relationship: Utilizes inheritance to achieve polymorphic behavior.
Interface Implementation: Interfaces in languages like Java allow for polymorphic behavior through method signatures.
Flexibility and Extensibility: Promotes flexibility in designing systems and allows for easy extension by adding new classes.
Enhances Code Readability: Enables more readable and understandable code through common interfaces and method names.
Example of Polymorphism
Let us understand further with some examples:
C++
C++
#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; }
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 are OOPs?
Object-Oriented Programming(OOPs) is a form of the journey of programming that 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.
What is the difference between inheritance and abstraction?
Inheritance establishes relationships where one class derives behaviors and properties from another, while abstraction focuses on defining a blueprint with relevant details hidden for implementation. Inheritance deals with "is-a" relationships, whereas abstraction focuses on "what it does" without implementation details.
Conclusion
In this article, we discussed about the difference between Inheritance and Polymorphism. We started with the introduction, understanding the key points, and benefits, and later looked at the difference table between them. If you wish to know more about Polymorphism, do check out Virtual Functions & Runtime Polymorphism in C++ and Inheritance in Java .