24 julio, 2024

Inheritance in programming: characteristics, types, examples

The inheritance in programming object oriented It is a way of organizing objects in a hierarchy from the most general to the most specific. It is the widget used to extend a class in another class, preserving an analogous execution.

In most class-based object-oriented languages, an object created through inheritance, called a child object, gets all the properties and behaviors of the parent object.

Therefore, it is a quality that represents the relationship between different classes. Inheritance allows one class to have the same behavior as another class and extend or tailor that behavior to provide a special action for specific needs.

Inheritance can help to represent objects that have some differences and some similarities in the way they work. You can put all the functionality that the objects have in common in a base class, and then define one or more subclasses with your own custom functionality.

[toc]

Heredity characteristics

Inheritance is one of the key features of object-oriented programming, which serves to define a new class from existing classes.

That is, new classes can be derived from existing base classes, adding new features. You don’t have to write from scratch. Therefore, inheritance provides a way to easily reuse existing code.

If you already have a class that does almost everything you want, you can create a subclass that partially overrides some of its behavior, or perhaps adds some new functionality.

A derived class inherits all the methods of the base class except the following:

– Constructors, destructors and copy constructors of the base class.

– Overloaded operators of the base class.

– Friendly functions of the base class.

Hierarchy

Just like a family tree does, inheritance serves to form a hierarchy of classes. It is important to note that attributes determined for a base class will automatically be present in the derived class. Also, the methods for the base class will work for the derived ones.

A class is said to be a subclass of the class from which it inherits. On the other hand, this class will be your superclass. The most generic class can be referred to as the base class, because it is at the bottom of the hierarchy.

Polymorphism

Inheritance is very popular in some statically typed languages, because it allows the programmer to bypass some of the static type restrictions.

An object that inherits from another object is considered a subtype of that object. An example might be the objects “instructor” and “student”, each of which inherits from “person”.

If instructor and student are of the same type person, you can write a function that accepts the type person as a parameter and that works on the instructor and student objects, because both are inheritors of person.

types of inheritance

single inheritance

When a derived class inherits the properties and behavior of a single base class. That is, a class extends to only one other class.

multilevel inheritance

Refers to the widget where you can inherit from a derived class, making this derived class the base class for a new class. That is, it results when a derived class is created from another derived class.

hierarchical inheritance

In this inheritance, many subclasses inherit from a single class. That is, it happens when a single base class is inherited by more than one derived class.

hybrid inheritance

It is the resulting inheritance when any of the three previous inheritances (single, hierarchical and multilevel) are combined.

multiple inheritance

Corresponds to the inheritance figure when a class inherits from more than one superclass. The drawback with this type of inheritance is that the derived class will have to deal with the dependency it has on two base classes.

On the other hand, this type of inheritance allows programmers to build classes that combine aspects of multiple classes and their corresponding hierarchies.

Inherit types from a base class

public inheritance

When you derive a class from a public base class, public members of the base class become public members of the derived class, and protected members of the base class become protected members of the derived class.

The private members of a superclass will never be directly accessible from a derived class, but can be accessed by calls to the public and protected members of the base class.

protected inheritance

When you create a class that inherits from a protected superclass, both public and protected members of this superclass will become protected members of the derived class.

private inheritance

When a subclass is created from a private superclass, the public and protected members of this superclass will become private members of the subclass.

examples

Example in Python

To show how the principle of inheritance works, we will take some robots or a better class of robots in the Python programming language. To do this, a class called MedicalRobot will be specified (PhysicianRobot), which is derived from the superclass Robot.

If you look at the code of the RobotMédico class, you can see that no attribute or method has been defined in this class.

Since the MedicalRobot class is a subclass of Robot, it inherits both the __init__ and say_hi methods in this case. Inheriting these methods means that they can be used as if they were defined in the MedicRobot class.

When an instance of MedicalRobot is created, the __init__ function will also create a name attribute. The say_hi method can be applied to the «y» object of RobotMédico, as can be seen in the result of the previous code.

Example in C++

A class can inherit from more than one class, which means that it can inherit data and functions from multiple superclasses. To define a derived class, a class derivation list is used to specify the base classes.

A class derivation list in C++ names one or more base classes and has the following form: classes derived-class: access-specifier base-classin which access-specifier is public, protected either privateand base-class is the name of a previously defined class. if not used access-specifierIs taken private default.

You can think of the superclass “Shape” and its derived class “Rectangle” as follows:

When the above code is compiled and executed, the following output is produced: “Total area: 35”.

References

Dave Braunschweig (2020). Inheritance and Polymorphism. Rebus Community. Taken from: press.rebus.community.
Python Course (2020). inheritance. Taken from: python-course.eu.
Data Mentor (2020). R Inheritance. Taken from: datamentor.io.
Tutorials Point (2020). C++ Inheritance. Taken from: tutorialspoint.com.
Net-informations (2020). Different Types of Inheritance. Taken from: net-informations.com.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *