Comparing Inheritance and Polymorphism

 

Comparing Inheritance & Polymorphism

 

In Object-oriented programming, polymorphism and inheritance are both fundamental concepts. The addition of objects in modern programming languages has made a significant difference in how we use the language and what we can do with it. In short , object-oriented programming is a set of  strategies that allows programmers to use categories and thus derive objects supported by these classes.

It aims to fit real-world entities and make it easier for programmers to write code that is inclusive of the paradigm in which they are writing code. There are four basic Object-oriented programming concepts: inheritance, abstraction, polymorphism, and encapsulation.

As previously stated, each of the ideas can be thought of as the pillars upon which any modern language stands. vs. inheritance and right of the bat, we tend to see a obtrusive distinction between the 2 concepts. Inheritance is the concept that allows the code to be reused again in the same or different program. We can even modify however the code behaves by keeping the properties we tend to like and discarding those that aren't helpful for the tasks we are attempting to accomplish. Inheritance saves tons of your time in developing nearly everything we see on our digital displays nowadays.

On the opposite hand, polymorphism is answerable for dictating the code that was already written and deciding what quite code must be dead supported specific parameters in real -time.

It would be beneficial for anyone to first go through each of these concepts before moving on to a discussion of the difference between polymorphism and inheritance. Once we fully understand what Inheritance and Polymorphism really means, the difference will be more clearer.

 

Inheritance

It would be a heinous crime in the programming paradigm to disregard inheritance as a critical OOP concept.write The significance of inheritance should never be underestimated because the goal of inheritance is "reusability." What inheritance does, as the name implies, is allow code written in one class to be extended into another.. So, in inheritance, there exists a base class; the class in which the code is written is to be reused.

To use all of the functions and variables associated with the base class, the next class we create must be inherited from it. When a class inherits the properties of another class (or takes the properties of another class), all of the members of the base class become members of the new derived class.

The sample code given below would show you what a general form of inheritance looks like. One thing to keep in mind is that the exact syntax you'd have to write to enable your code's inheritance is entirely dependent on the programming language you use.


class derived-class-name : access-specifier base-class-name{

body of the derived class

}

 

Certain aspects of the preceding example require clarification. The term "access  specifier" refers how the derived class would access the properties and methods of the base class. There are three access specifiers in general, each having their own meaning (namely private, public, and protected) and properties.

Once again, it depends upon the language of your choice, you might or might not have to use these access specifiers. So, in the language 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. 

 

When it comes to inheritance, C++ also provides a chance of options. You will find some of them listed below:

 

a.    Hierarchical Inheritance: This type of inheritance followed rule of they should only be one superclass, and from that superclass, there must be many derived subclasses.  

     Example below will show how it actual works

 package Example_Inheritance;


class  VIT{
void  college()

{System.out.println("Good  college...");}
}
class Sdp  extends VIT{
void present()

{

System.out.println("Software  development...  ");}
}
class Edi extends VIT{
void exhibit()

{

System.out.println("hardware  development...");}
}
public  class  TestInheritance3{
public static void main(String args[]){
Edi c=new Sdp();

Sdp d = new Sdp();
c.present();
d.exhibit();
}

}


Output : 

Software  development... 

hardware  development...


b. Multilevel Inheritance: It is a chain of inheritance of classes.

Example given below will explain multiple inheritance:

Multiple Inheritance;

 

class VIT{

void tp()

System.out.println("Presentations  are starting...");

}

class  Sdp extends VIT{

void present() 

{

System.out.println("Sdp will have a software project...");}

}

class Edi extends Sdp{

void exhibit()

{

System.out.println("Edi will have an exhibition...");}

}

public class TestInheritance{

public static void main(String args[])

 

{

 

Edi d=new Edi(); 

d.tp(); 

d.present();

d.exhibit()

}

}


Output:

Presentations  are starting...

Sdp will have a software project...

 Edi will have an exhibition...


c. Single Inheritance: This is perhaps the simplest form of inheritance. There is just one base class and one derived class.

 Example given below will give the idea how single inheritance works 


class  VIT{
void tp() 

{

System.out.println("Presentation  will  start...");}
}
class  Presentation  extends VIT{
void  present()

{

System.out.println("Presentation  Try this will include edi,sdp,gd,cp ...");}
}
public class  TestInheritance

{
public static void main(String args[]){
Presentation d=new Presentation();
d.tp();
d.present();

}

} 

Output :

Presentation will start...

Presentation will include edi,sdp,gd,cp and life goes on and on...

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 have the meaning of one function but many forms. Polymorphism This occurs during the compilation process. Polymorphism is only possible at compile time due to the concept of overloading, but it is a reality at run time due to the feature of overriding. Let us go over the definitions of overloading and overriding one by one. 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 chosen. 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.

 

class overload{

int a, b;

public:

int overload(int x){ // first overload() constructor

 a=x;

return a;

}

int over burdening (int x, int y) /second overload()  constructor

a=x;

b=y;

return a*b;

}

};

int main() overload O1; O1.overload(20); 

/this is the first overload() constructor call. O1.overload

(20,40); // second overload() constructor call

 

 

Here in this example, we see overloading in action. Examine how different constructors are called depending on whether the final value of the object's parenthesis is one or two integers.

Consider the definition of Overriding, which is only possible for inherited functions.

Yes, inheritance is necessary for function overriding. 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:

 

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;

}

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 differences between polymorphism and inheritance:

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.

 

Difference between Inheritance and Polymorphism:

 

INHERITANCE

POLYMORPHISM

1.

Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class).

Whereas polymorphism is that which can be defined in multiple forms.

2.

It is basically applied to classes.

Whereas it is basically applied to functions or methods.

3.

Inheritance supports the concept of reusability and reduces code length in object-oriented programming.

Polymorphism allows the object to plan which form of the function to implement at compile-time (overloading) as well as run-time (overriding).

4.

Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance.

Whereas it can be compiled-time polymorphism (overload) as well as run-time polymorphism (overriding).

5.

It is used in pattern designing.

While it is also used in pattern designing.

 Source: https://www.geeksforgeeks.org/

 

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.

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.

 


References:

https://www.geeksforgeeks.org/

https://blog.devgenius.io/

 

Research team

  1. Rohan Mahjan
  2. Tejas Mahajan
  3. Henna Kannake
  4. Zuben Khan
  5. Karan Late

 


Comments

  1. Very informative and useful to all thanks to giving us such a great information.

    ReplyDelete
  2. Informative one.. Keep on writing more such blog πŸ‘ŒπŸΌπŸ‘πŸΌ

    ReplyDelete
  3. Great work guys waiting for next one

    ReplyDelete
  4. Useful one, Thanks for the blog!!

    ReplyDelete
  5. Great work. Keep posting such informative blogs.πŸ‘

    ReplyDelete

Post a Comment