Inheritance allows, code reusability and the polymorphism is, the occurrence of one function with different form. The basic difference between inheritance and polymorphism is that inheritance allows the already existing code to be reused again in a program, and polymorphism provides a mechanism to dynamically decide what form of a function to be invoked.
Content: Inheritance Vs Polymorphism
Comparison Chart
Basis for Comparison | Inheritance | Polymorphism |
---|---|---|
Basic | Inheritance is creating a new class using the properties of the already existing class. | Polymorphism is basically a common interface for multiple form. |
Implementation | Inheritance is basically implemented on classes. | Polymorphism is basically implemented on function/methods. |
Use | To support the concept of reusability in OOP and reduces the length of code. | Allows object to decide which form of the function to be invoked when, at compile time(overloading) as well as run time(overriding). |
Forms | Inheritance may be a single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance. | Polymorphism may be a compile time polymorphism (overloading) or run-time polymorphism (overriding). |
Example | The class 'table' can inherit the feature of the class 'furniture', as a 'table' is a 'furniture'. | The class 'study_table' can also have function 'set_color()' and a class 'Dining_table' can also have function 'set_color()' so, which form of the set_color() function to invoke can be decided at both, compile time and run time. |
Definition of Inheritance
Inheritance is one of the crucial features of OOP, which strongly support “reusability”. Reusability could be described as creating a new class by reusing the properties of the existing class. In inheritance, there is a base class, which is inherited by the derived class. When a class inherits any other class, the member(s) of the base class becomes the member(s) of a derived class.
The general form of inheriting a class is as follow:
class derived-class-name : access-specifier base-class-name{ // body of the class } ;
Here, the access specifier provides the mode of access (private, public, protected) to the member(s) in the base class to the derived class. If no access specifier is present, by default, it is considered as “private”. In C++, if the derived class is “struct” then the access specifier is “public” by default.
In C++, inheritance can be achieved in five forms. They can be classified as:-
- Single Inheritance (only one super class )
- Multiple Inheritance ( several superclasses )
- Hierarchical Inheritance ( one super class, many subclasses)
- Multiple Inheritance ( derived from a derived class)
In Java, the class inherits the other class by using the keyword “extends”. In Java, the base class is referred as a super class, and derived class is referred as a subclass. A subclass can not access those members of the base class, which are declared as “private”. The general form inheriting the class in Java is as follow.
class derived-class-name extends base-class-name{ // body of the class } ;
Java does not support the inheritance of multiple inheritance, whereas it supports multilevel hierarchy. In Java, sometimes a super class may want to hide its implementation details, and it makes some part of that data “private”.
As in Java, a subclass can not access the private members of the superclass and if a subclass wants to access or initialize those members, then Java provides a solution. The subclass can refer the members of its immediate superclass by using a keyword “super”. Remember, you can only access the members of the immediate superclass.
The ‘super’ has two general forms. The first is, it use to call the constructor of super class. The second is, to access the member of the superclass that has been hidden by the member of the subclass.
//first form of calling the constructor. class supper_class{ supper_class(argument_list){..} //constructor of super class }; class sub_class extends supper_class { sub_class(argument_list){..} //constructor of sub_class super(argument_list); // sub_class calls the constructor of super class } };
//second for of 'super' class supper_class{ int i; } class sub_class extends supper_class { int i; sub_class(int a, int b){ super.i=a; // 'i' of super class i=b; // 'i' of sub class } };
Definition of Polymorphism
The term polymorphism simply means ‘one function, multiple forms’. Polymorphism is achieved at both compile time and run time. The compile time polymorphism is achieved through “overloading” whereas, the run time polymorphism is achieved through “overriding”.
The polymorphism allows the object to decide “which form of the function to be invoked when” at both, compile time and run time.
Let’s discuss the first concept of overloading. In overloading, we define a function in class more than one time with different, data type and the number of parameters whereas the function to be overloaded must have the same return type. Most of the times the functions to the overloaded are constructors of the class.
class overload{ int a, b; public: int overload(int x){ // first overload() constructor a=x; return a; } int overload(int x, int y){ //second overload() constructor a=x; b=y; return a*b; } }; int main(){ overload O1; O1.overload(20); //first overload() constructor call O1.overload(20,40); // second overload() constructor call }
Now, let’s discuss the second form of polymorphism, i.e. overriding. The concept of overriding can only be implemented to the function of the classes which also implement the concept of inheritance. In C++, the function to be overridden is preceded by the keyword “virtual” in the base class and redefined in the derived class with the same prototype except the keyword “virtual”.
class base{ public: virtual void funct(){ //virtual function of base class cout<<"This is a base class's funct()"; } }; class derived1 : public base{ public: void funct(){ //virtual function of base class redefined in derived1 class cout<<"This is a derived1 class's funct()"; } }; int main() { base *p, b; derived1 d1; *p=&b; p->funct(); //call to base class funct(). *p=&d1; return 0; }
Key Differences Between Inheritance and Polymorphism
- Inheritance is creating a class that derives its feature from an already existing class. On the other hand, polymorphism is an interface that can be defined in multiple forms.
- Inheritance is implemented on the classes whereas, the polymorphism is implemented on methods/functions.
- As inheritance allows a derived class to use the elements and methods defined in the base class, the derived class does not need to define those elements or method it again so, we can say it increases code reusability and hence, reduces the length of the code. On the other hand, polymorphism makes it possible for an object to decide what form of the method it wants to invoke at both compile-time and run time.
- The inheritance can be classified as single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance. On the other hand, polymorphism is classified as overloading and overriding.
Conclusion
The Inheritance and polymorphism are interrelated concepts, as the dynamic polymorphism applies to the classes which also implement the concept of inheritance.
Anilkumar says
Nice explanation…😀
Pankaj Dutta says
Nice
Mohit prasad says
I love this site…..nice content and explanation is perfect for exam point of view 😀😀
vinod kumar says
wonderful explanation
Manoj Kumar jena says
so..Good explain
im very Happy..
Ulviyya says
Thank a lot. I super understood polymorphism vs inheritance first time
Ulviyya says
Thank for excellent explains