Difference between inheritance and polymorphism

Difference between inheritance and polymorphism

The inheritance allows it, the reuse of the code and the polymorphism is, the occurrence of a function with a different form. The basic difference between inheritance and polymorphism is that inheritance allows the existing code to be reused again in a program, and polymorphism provides a mechanism to dynamically decide what form of a function is invoked.

Comparative graph

Basis for comparison Herencia Polymorphism
BASIC Inheritance is creating a new class using the properties of the existing class. Polymorphism is basically a common interface for multiple forms.
Implementation Inheritance is basically implemented in classes. Polymorphism is basically implemented in function / methods.
Use To support the concept of reuse in OOP and reduce the length of the code. It allows the object to decide what form of the function is invoked when, at compile time (overload), as well as execution time (override).
Shapes The inheritance can be a unique inheritance, multiple inheritance, multi-level inheritance, hierarchical inheritance and hybrid inheritance. The polymorphism can be a compilation time (overhead) polymorphism or a runtime (cancellation) polymorphism.
Example The class 'table' can inherit the characteristic of the 'furniture' class, since a 'table' is a 'furniture'. The class 'study_table' can also have the function 'set_color ()' and a class 'Dining_table' can also have the function 'set_color ()', so the shape of the function set_color () can be decided both at the time compilation how to run time

Inheritance Definition:

Inheritance is one of the crucial characteristics of the OOP, which strongly supports "reuse". Reuse 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 way to inherit a class is as follows:

 clase nombre-clase-derivada: nombre-clase-base-especificador de acceso {// cuerpo de la clase}; 

Here, the access specifier provides the access mode (private, public, protected) to the member (s) in the base class to the derived class. If there is no access specifier present, by default, it is considered as "private". In C ++, if the derived class is "struct", the access specifier is "public" by default.

In C ++, inheritance can be achieved in five ways. They can be classified as:

  • Unique inheritance (only one super class)
  • Multiple inheritance (several superclasses)
  • Hierarchical inheritance (one class sper, many subclasses)
  • Multiple inheritance (derived from a derived class)

In Java, the class inherits the other class using the "extend" keyword. In Java, the base class is known as a sper class and the derived class is called a subclass. A subclass cannot access those members of the base class, which are declared as "private". The general form inherited by the class in Java is as follows.

 clase nombre-clase-derivado extiende nombre-clase-base {// cuerpo de la clase}; 

Java does not support multiple inheritance inheritance, while it supports multi-level hierarchy. In Java, sometimes a super class can hide its implementation details, and makes some of that data "private." As in Java, a subclass cannot access the private members of the superclass and if a subclass wants to access or initialize those members, then Java offers a solution. The subclass can refer to the members of its immediate superclass using a "super" keyword. Remember, you can only access members of the immediate superclass.

The 'super' has two general forms. The first is, it is used to call the constructor of the sper class. The second is, to access the superclass member that has been hidden by the subclass member.

 // primera forma de llamar al constructor. class supper_class {supper_class (argument_list) {..} // constructor de super clase}; class sub_class extiende supper_class {sub_class (argument_list) {..} // constructor de sub_class super (argument_list); // sub_class llama al constructor de la superclase}}; 
 // segundo para de 'super' class supper_class {int i; } class sub_class extiende supper_class {int i; sub_class (int a, int b) {super.i = a; // 'i' de la super clase i = b; // 'i' de la subclase}}; 

Definition of polymorphism

The term polymorphism simply means "one function, multiple forms." Polymorphism is achieved both at compile time and at runtime. The compilation time of the polymorphism is achieved through the "overload", while the execution time of the polymorphism is achieved through the "override".

Polymorphism allows the object to decide "what form of the function to be invoked when", both at compile time and at execution time. Let's discuss the first concept of overload. In overload, we define a function in the class more than once with different types of data and the number of parameters, while the function to be overloaded must have the same type of return. Most of the time the overloaded functions are constructors of the class.

 sobrecarga de clase {int a, b; public: int overload (int x) {// primer overload () constructor a = x; devuelve un } sobrecarga de int (int x, int y) {// segundo constructor de sobrecarga () a = x; b = y; devuelve a * b; }}; int main () {sobrecarga O1; O1.overload (20); // primera sobrecarga () llamada de constructor O1.overload (20, 40); // segunda sobrecarga () llamada de constructor} 

Now, let's discuss the second form of polymorphism, that is, cancel. The concept of cancellation can only be implemented in the function of the classes that also implement the concept of inheritance. In C ++, the function to be overridden is preceded by the "virtual" keyword in the base class and redefined in the derived class with the same prototype, except for the "virtual" keyword.