When it comes to developing code for apps, websites, or any other software product, time is a key resource in the task. It quickly becomes a crucial factor that can make the difference between a pleasant and easy-to-carry project, or a real nightmare.
However, in our area, fast is not synonymous with easy. It is important to have an order in what we do, even if it takes us a little more time, in this way we ensure that in the end, everything responds as it is supposed to. When it comes to code, a small mistake can cause us to damage our entire work, so keeping it neat and clean is a priority.
There are things we can do to make our chores easier for us, and the main one is code reuse. This simplifies on a large scale the action of designing, creating, and maintaining all our software projects. It is in this process that the concept of Inheritance comes into play.
What is inheritance in programming:
Inheritance is a concept used when we talk about code reuse that involves creating new classes from previously created classes. What happens is that the new class INHERITS functions and attributes of an existing class.
Putting it in a visual example, we can take as a base any product that we use in our daily life such as a coffee maker. The first coffee maker that the world knew was Caféolette, a cylinder with a plate full of holes that filtered the ground coffee, from which the industrial coffee maker is derived with variations in its form and operation but with the same beginning and end.
From them, all the coffee machines that we know today emerged. Some have new functions in them or new ways of preparing coffee, but we continue to call them coffee makers, all with their differences but starting from the same place.
Now that we have a visual example we can see what this definition means in our code.
Inheritance in the code:
Let's take as an example a virtual library-type app, where each user can open their accounts and organize their readings. For example, we create the class for our user, a person who can access the library and read the content, but we also need a user type only for the staff that works in the app.
As this user requires different functionalities and attributes, what we will do is create a new class that inherits all the info from the class that already existed, and give it the corresponding name. In this case, it would be staff, and from that already existing class we begin to give it new responsibilities.
How does inheritance work?
In order to reuse the code of the classes to create new objects, we have the parent classes and the child classes. As in a family, it is the child class that inherits the code from the parent class to adapt it to what is required.
As in real life, a child inherits characteristics from his parents without ceasing to have its own essence, it is not the same or a copy, but it comes from another existing class.
If one class derives from another, it inherits its functions and attributes, then you can add new attributes, functions, or redefine the inherited ones.
Code reuse is a resource that we must use, things can get a bit complex doing it, resulting in dirty, messy, and long code.
Knowing the reality of the concepts that we apply makes us understand how to use them properly.
What are your tips to reuse code successfully?