Inheritance (computing)

format_list_bulleted Contenido keyboard_arrow_down
ImprimirCitar

In object-oriented programming, inheritance is, after aggregation or composition, the most widely used mechanism to achieve some of the most precious goals in software development such as reuse and the extensibility. Through it, designers can create new classes starting from a pre-existing class or hierarchy of classes (already checked and verified), thus avoiding the redesign, modification and verification of the already implemented part. Inheritance makes it easy to create objects from existing ones and implies that a subclass gets all the behavior (methods) and finally the attributes (variables) of its superclass.

It is the relationship between a general class and a more specific class. For example: If we declare a paragraph class derived from a text class, all the methods and variables associated with the text class are automatically inherited by the paragraph subclass.

Inheritance is one of the mechanisms in class-based object-oriented programming languages by which one class is derived from another in a way that extends its functionality. The class from which it inherits is often called the base class, parent class, superclass, ancestor class (the vocabulary that is used usually depends to a great extent on the programming language).

In languages that have a strong type system and strictly restrict the data type of the variables, inheritance is usually a fundamental requirement to be able to use Polymorphism, as well as a mechanism that allows to decide at time of execution which method should be called in response to receiving a message, known as late binding or dynamic binding.

Example in Java

import Javax. ♪;import javax.swing.JOptionPane;public class Mamifer{ private int legs; private String Name; public void PrintPatas(){ JOptionPane.showMessageDialog(null," He has. + legs + "Pass", "Mommy", JOptionPane.INFORMATION_MESSAGE); ! public Mamifer(String Name, int legs this.Name = Name; this.legs = legs; !!public class Dog extensions Mamifer { public Dog(String Name super(Name, 4); !!public class Gato extensions Mamifer { public Gato(String Name super(Name, 4); !!public class Create Dog { public static void main(String[] args) { Dog puppy = new Dog("Pantaleon"); puppy.PrintPatas(); He's in the mammal class. !!

The classes mammals, cat and dog are declared, making cat and dog be ones of mammals (derived from this class), and it is seen how through them the animal is named but also like this legs is accessed by giving it the default value for that species.

It is important to highlight three things. The first is that inheritance is not an essential mechanism in the object-oriented programming paradigm; In most prototype-based object-oriented languages, classes do not exist, so inheritance does not exist, and polymorphism is achieved by other means. Second, the preferred means of achieving extensibility and reusability goals is aggregation or composition. The third is that in languages with a weakly typed system, polymorphism can be achieved without using inheritance.

On the other hand, and although inheritance is not an indispensable concept in the object-oriented programming paradigm, it is much more than a mechanism of class-based languages, because it implies a way of reasoning about how to design certain parts of a program. In other words, it is not only a mechanism that allows a design to be implemented, but also establishes a conceptual framework that allows reasoning on how to create that design.

Abstract Class

Inheritance allows classes to exist that will never be directly instantiated. In the example above, a "dog" would inherit the attributes and methods of the "mammal" class, as well as "cat", "dolphin" or any other subclass; but, in execution, there will be no "mammal" that does not belong to any of the subclasses. In that case, such a class would be known as an Abstract Class. The absence of specific instances is its only peculiarity, for everything else it is like any other class.

Inheritance and information hiding

In certain languages, the designer can define which instance variables and methods of objects of a class are visible. In C++ and Java this is achieved with the private, protected and public specifications. Only variables and methods defined as public on an object will be visible by all objects. In other languages like Smalltalk, all instance variables are private and all methods are public.

Depending on the language being used, the designer can also control which members of the superclasses are visible in the subclasses. In the case of Java and C++ the access specifiers (private, protected, public) of the members of the superclass also affect inheritance:

Private
No private member of the superclass is visible in the subclass.
Protected
The protected members of the superclass are visible in the subclass, but not visible to the outside.
Public
Public members of the superclass remain public at the subclass.

Redefinition of methods

In the derived class you can override an existing method in the base class, in order to provide a different implementation. To redefine a method in the subclass, simply declare it again with the same signature (name and parameters). If a certain method of an object is called that is not defined in its own class, the search is triggered up the hierarchy to which that class belongs. However, if there were two methods with the same signature, one in the class and one in a superclass, the one in the class would be executed, not the one in the superclass.

When a method is redefined in a class, it is possible to explicitly access the original method of its superclass, using a specific syntax that depends on the programming language used (in many languages this is the keyword super).

Advantages

  • Help programmers save code and time, as the parent class has been implemented and verified previously, subtracting only reference from the class derived to the base class (which is usually implemented and verified earlier, extensions, inherits, subclass or other similar keywords, depending on language.
  • Objects can be built from similar ones. This requires a basic class (which can even be part of a wider class hierarchy).
  • The derivative class inherits the behavior and attributes of the base class, and it is common for it to add its own behavior or to modify the inherited.
  • Every class can serve as a base class to create others.

Disadvantages

If the class hierarchy is too complex, the programmer may have trouble understanding how a program works. In addition, it can become more complex to detect and resolve programming errors, for example when modifying a parent class that affects the behavior of the subclasses.

Another problem is that subclasses must be defined in code, so users of the program cannot define new subclasses. Other design patterns allow users to define variants of an entity at run time.

Inheritance stereotypes

Simple wound
One class can only inherit from one base class and no other.
Multiple strength
A class can inherit the characteristics of several base classes, i.e. it may have several parents. In this aspect there are discrepancies among language designers. Some of them have preferred not to admit multiple inheritance because potential conflicts between methods and variables with the same name, and eventually with different behaviors creates a cognitive mismatch that goes against the principle of object-oriented programming. Therefore, most object-oriented languages support simple inheritance. In contrast, a few languages admit multiple heritage, including C++, Python, Eiffel, while Smalltalk, Java, Ada and C# only allow simple inheritance.
  • Wd Data: Q212542

Contenido relacionado

Internet Assigned Numbers Authority

Internet Assigned Numbers Authority is the entity that oversees the global allocation of IP addresses, autonomous systems, DNS domain name root servers, and...

Space Shuttle Naming Designation

NASA Space Shuttle Mission...

Depth of field

By depth of field or PDC is traditionally understood in optics, and in photography in particular, the area that includes from the closest point and the...
Más resultados...
Tamaño del texto:
undoredo
format_boldformat_italicformat_underlinedstrikethrough_ssuperscriptsubscriptlink
save