contador javascript Saltar al contenido
Contact : alejandrasalcedo0288@gmail.com

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
BASICInheritance is creating a new class using the properties of the existing class.Polymorphism is basically a common interface for multiple forms.
ImplementationInheritance is basically implemented in classes.Polymorphism is basically implemented in function / methods.
UseTo 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).
ShapesThe 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.
ExampleThe 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.

 class base {public: virtual void funct () {// virtual function of base class cout << "Este es el funct de una clase base ()"; }}; class Derivada1: base pblica {public: void funct () {// funcin virtual de la clase base redefinida en derivada1 clase cout 

Diferencias clave entre herencia y polimorfismo

  1. La herencia es crear una clase que deriva su caracterstica de una clase ya existente. Por otro lado, el polimorfismo es una interfaz que se puede definir en mltiples formas.
  2. La herencia se implementa en las clases, mientras que el polimorfismo se implementa en los mtodos / funciones.
  3. Como la herencia le permite a una clase derivada usar los elementos y mtodos definidos en la clase base, la clase derivada no necesita definir esos elementos o usar el mtodo nuevamente, as podemos decir que aumenta la capacidad de reutilizacin del cdigo y, por lo tanto, reduce la longitud del cdigo. . Por otro lado, el polimorfismo hace posible que un objeto decida qu forma del mtodo desea invocar tanto en tiempo de compilacin como en tiempo de ejecucin.
  4. La herencia se puede clasificar como herencia simple, herencia mltiple, herencia multinivel, herencia jerrquica y herencia hbrida. Por otro lado, el polimorfismo se clasifica como sobrecarga y anulacin.

Conclusin:

La herencia y el polimorfismo son conceptos interrelacionados, ya que el polimorfismo dinmico se aplica a las clases que tambin implementan el concepto de herencia.