Polymorphism vs. Inheritance: Difference Between Polymorphism & Inheritance [2021]

Polymorphism and inheritance are both very fundamental concepts of Object-oriented programming. The addition of objects in the modern-day programming languages has made a considerable difference in the way we use the language and the things we are capable of doing with them. In a nutshell, object-oriented programming is a set of methods that allows the programmer to use classes and, hence, derive objects based on these classes.

It aims to resemble the real-world entities and make it easier for programmers to make their code inclusive of the paradigm they are writing their code in. There are essentially four Object-oriented programming concepts, namely Inheritance, Abstraction, Polymorphism, and encapsulation. Now, each of the ideas, as mentioned earlier, can be thought of as the pillars on which any modern-day language stands. 

Coming back to our topic of polymorphism vs. inheritance and right of the bat, we see a glaring difference between the two concepts. Inheritance is the concept that allows the code to be reused again in the same or different program. We can even change how the code behaves by keeping the things we like and discarding the ones that are not useful for the tasks we are trying to accomplish. Inheritance saves a lot of time in developing almost everything we see on our digital displays nowadays.

On the other hand, polymorphism is responsible for dictating the code that was already written and deciding what kind of code needs to be executed based on specific parameters in real-time. It would be beneficial for anyone to first go through each of these concepts in detail first, before moving towards the discussion of the difference between polymorphism and inheritance. The differences will become more pronounced once we know what exactly is meant by Inheritance and Polymorphism.

Inheritance

It would be a crime in the programming paradigm to not consider Inheritance to be a highly crucial OOP concept. The importance of inheritance should never be treated lightly because the aim of inheritance is “reusability.” What inheritance does, as the name would imply, is that it allows the code written in a class to be extended into another class. So, in inheritance, there exists a base class; the class in which the code is written is to be reused.

The next class that we would create would have to be inherited from this base class to use all the functions and variables associated with the base class. Whenever a class takes the properties of another class (or inherits from a different class), all the members who are present in the base class become a member of this new derived class. 

The sample code given below would show you what a general form of inheritance looks like. One key thing to note here is that the exact syntax you would have to write to enable your code’s inheritance would solely depend on the programming language you choose. 

  1. class derived-class-name : access-specifier base-class-name{
  2. // body of the derived class 
  3. }

In the example shown above, certain things need a little explanation. The word “access specifier” means the way in which the derived class would access the base class’s properties and methods. There are three access specifiers in general, each having their own meaning (namely private, public, and protected) and properties.

Once again, depending on the language of your choice, you might or might not have to use these access specifiers. So, in the world of C++, if you inherit without specifying anything by default, it goes private. However, if you inherit from a structure (goes by the keyword struct), the default access specifier would be public instead of private. 

Read: Career Opportunities in R Programming Language

C++ also gives you a lot of options to choose from when you are doing inheritance. You will find some of them listed below:

a. Hierarchical Inheritance: This type of inheritance follows the rule that there should only be one superclass, and from that superclass, there must be many derived subclasses. You will find an example of it below: 

  1. //Base Class
  2. class A
  3. {
  4.  public void funA()
  5.  {
  6.  //
  7.  }
  8. }
  9.  
  10. //Derived Class
  11. class B: A
  12. {
  13.  public void funB()
  14.  {
  15.  //
  16.  }
  17. }
  18.  
  19. //Derived Class
  20. class C: A
  21. {
  22.  public void funC()
  23.  {
  24.  //
  25.  }
  26. }
  27.  
  28. //Derived Class
  29. class D: C
  30. {
  31.  public void funD()
  32.  {
  33.  //
  34.  }
  35. }
  36.  
  37. //Derived Class
  38. class E: C
  39. {
  40.  public void funE()
  41.  {
  42.  //
  43.  }
  44. }
  45.  
  46. //Derived Class
  47. class F: B
  48. {
  49.  public void funF()
  50.  {
  51.  //
  52.  }
  53. }
  54.  
  55. //Derived Class
  56. class G: B
  57. {
  58.  public void funG()
  59.  {
  60.  //
  61.  }
  62. }

b. Multiple Inheritance: If you are doing multiple inheritance, that would mean that you have just one derived subclass which is inheriting from multiple superclasses. You will find a simple example of multiple inheritance below: 

  1. //Base Class
  2. class A 
  3. {
  4.  public void funA()
  5.  {
  6.  //
  7.  }
  8. }
  9.  
  10. //Base Class
  11. class B
  12. {
  13.  public void funB()
  14.  {
  15.  //
  16.  }
  17. }
  18.  
  19. //Derived Class
  20. class C: A, B
  21. {
  22.  public void funC()
  23.  {
  24.  //
  25.  }
  26. }

c. Single Inheritance: This is perhaps the simplest form of inheritance. There is just one base class and one derived class. You will find an example below: 

  1. //Base Class
  2. class A 
  3. {
  4.  public void funA()
  5.  {
  6.  //TO DO:
  7.  }
  8. }
  9.  
  10. //Derived Class
  11. class B: A
  12. {
  13.  public void funB()
  14.  {
  15.  //TO DO:
  16.  }
  17. }

Polymorphism

The basic definition of the word polymorphism means having many forms. This definition holds very accurately in explaining polymorphism in the context of programming. In this paradigm, polymorphism takes the meaning of one function but many forms. Polymorphism actually happens at compile time. Polymorphism at compile time is only possible because of the concept of overloading, and at run time, the feature of overriding makes polymorphism a reality. One by one, let us tackle the definition of both overloading and overriding. 

Overloading requires the code that you write or the class’s function to be written more than once with different parameters but having the same return type. It means that the arguments that you pass into the function can be different, and just by looking at the final values which are passed into the function at run time, which form of the function is to be called is decided. Generally, we see the class constructor be the most overloaded function. All this theory will become much clear, and it will be easier for you to ingrain it in your mind with the help of an example.

  1. class overload{
  2. int a, b;
  3. public:
  4. int overload(int x){ // first overload() constructor
  5. a=x;
  6. return a;
  7. }
  8. int overload(int x, int y){ //second overload() constructor
  9. a=x;
  10. b=y;
  11. return a*b;
  12. }
  13. };
  14. int main(){
  15. overload O1;
  16. O1.overload(20); //first overload() constructor call
  17. O1.overload(20,40); // second overload() constructor call

Here in this example, we see overloading in action. Look at how different constructors are called depending on whether the object’s parenthesis’s final value is either one integer or two.

Let us tackle the definition of Overriding next. You can only perform overriding to those specific functions which are inherited. Yes, inheritance is a key point in making overriding of functions possible. If you want to write a function and also override it, in C++ you will have to use the keyword virtual before the function definition, and in the derived class using the same name for your function, just remove the virtual keyword. To solidify your understanding, here is an example:

  1. class base{
  2. public:
  3. virtual void funct(){ //virtual function of base class
  4. cout<<“This is a base class’s funct()”;
  5. }
  6. };
  7. class derived1 : public base{
  8. public:
  9. void funct(){ //virtual function of base class redefined in derived1 class
  10. cout<<“This is a derived1 class’s funct()”;
  11. }
  12. };
  13. int main()
  14. {
  15. base *p, b;
  16. derived1 d1;
  17. *p=&b;
  18. p->funct(); //call to base class funct().
  19. *p=&d1;
  20. return 0;
  21. }

Look at how the keyword virtual is used in the base class, and in the derived class, the same function definition is there just the keyword virtual is absent.

Some sticking difference between polymorphism and inheritance:

  1. Inheritance is essentially making a class, and then having other classes in your program get their feature form the already existing base class. However, polymorphism is an interface, and because of being an interface, it can take different shapes and forms.
  2. Inheritance is a property pertaining to just classes whereas, polymorphism extends itself into any method and/or function.
  3. Inheritance allows the derived class to use all the functions and variables declared in the base class without explicitly defining them again. That is why we say that inheritance increases the code reusability and reduces the length of code, which we would have to write if the inheritance was absent. Whereas, polymorphism allows for the same function name to have two very different codes. So, in a sense, instead of reducing the length of the code which we would have to write, polymorphism is extending it further.
  4. There are many forms that inheritance can take; you can be really creative with inheritance. However, polymorphism can only be accomplished by two means, i.e., overloading and overriding. You can still go very crazy while using polymorphism, but you are restricted to just the two ways of implementing it into your writing code.

Must Read: Must Read 47 OOPS Interview Questions & Answers For Freshers & Experienced

Polymorphism vs. inheritance: Tabular differentiation

In the table below, you will find a clear difference between polymorphism and inheritance:

COMPARATIVE MEASURES INHERITANCE POLYMORPHISM
Fundamental differences between the two The meaning of inheritance is to create new classes which have properties (functions and variables of the existing classes) It is essentially a platform that allows the code to be written in different forms. 
Differences in the way both of them could be integrated into your code Only classes can enjoy real inheritance in the code. It can be implemented and used by any functions and/or methods throughout the entire code. 
Differences in the way both of them could be used. It enables the code which is written to be reused within the same or different program. Without it, Object-oriented programming would be missing a crucial feature. It allows the declared object to make the decision of which form of the function is to be called. There are two times when this can be decided. At run-time, it is called overriding; at compile-time, it is called overloading.
Different forms both of them can take There are a plethora of forms that inheritance can take.  There can only be two forms of polymorphism. Depending on the time in the program, it changes from overloading to overriding.
A basic example demonstrating how each of them is implemented. The class bike can inherit from the class of two-wheel vehicles, which in turn could be a subclass of vehicles. The class bike can have a method named set_color(), which changes the bike’s color based on the name of the color you have entered. 

 

Check out: What is Type Casting in Java | Understanding Type Casting As a Beginner

Conclusion

It is safe to say that both polymorphism and inheritance are critical concepts in making any program a reality. They both are the foundation on which the idea of object-oriented programming was laid. There are many differences between polymorphism and inheritance because they serve two very different purposes.

Polymorphism allows the programmer to write multiple definitions of a function. At the same time, inheritance enables the user to reuse the already written code. To fully understand and appreciate the two concepts, it is recommended that you further read on both the topics.

One thing which you should always keep in your mind whenever you are writing code is that if you are looking to refactor the code which you have already written (basically the definition of one class and use it again in your code to serve a similar or different purpose), you should make use of inheritance. If you are looking to reduce the overall confusion in your code and want to use the same name of the function to do similar tasks, you should use polymorphism.

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Enroll Today

Leave a comment

Your email address will not be published.

×