Software development is strongly associated with real-world entities. However, we should consider not only the entities themselves but also the interactions between them. One of the ways it is presented in object-oriented programming is inheritance.
In this topic, we will talk about this technique and see how it can be useful in your code.
What is inheritance?
We start making our program with basic objects and methods. Imagine a messenger application. The main objects in messengers are chats, so we start ours with the CHAT class. Then, we decide to break it into three categories:
- direct messages;
- group chat;
- saved messages only for yourself.
If we want to make more types of chats, do we need to write the code from scratch for each one? The answer is no! With the help of inheritance, we can reuse the code we already have.
Inheritance is a relation between entities that we interpret as "is a" or "is a kind of" relation. In the case of programming methods and attributes, it means that a child entity has every feature of the parent entity. However, we can also change inherited features or define new ones for child classes.
Here are some examples of inheritance:
-
A swan is a bird. A swan can do everything that a typical bird can do, but it has an additional feature: it can swim.
-
A Python developer is a programmer. They, like all programmers, develop algorithms and computer programs based on special mathematical models, but they do it using a specific language, namely Python.
- A laptop is a computer. It has processor units, memory, a keyboard, and all the stuff computers typically have, but it is also portable.
Inheritance modeling
Let's stick to the example with a chat and see how we can model the classes of our program.
We can start by moving all the shared methods and attributes to the base CHAT class. For the sake of simplicity, we define only one attribute and only one method:
To make our base class more useful, we can add some more features like SEND_FILE, EDIT_MESSAGE, POSTPONE_MESSAGE, and others.
Next, we draw child classes and methods that need to work properly:
Let's take a closer look at the diagram and figure out what benefits inheritance gives us. First, we don't need to implement any other methods to make a new class SAVED_MESSAGE: we add only one participant there, and it can work right out of the box! For the other two classes, we implement only some new methods like FETCH_MESSAGES to get updates from other participants and ADD_PARTICIPANT to add a new person to a group chat.
You may notice that we can even inherit the GROUP_CHAT from the DIRECT_MESSAGES to reuse its methods. It makes sense because there is not much difference if two, three, or a hundred persons communicate in a chat, but the hierarchy is fine, too.
Conclusion
Inheritance is a very flexible and useful mechanism in software development. It allows us to simplify and speed up the development process by reusing the code we've already written. As you have learned, it also reflects the dependency between the parent and child classes that we can express as "a kind of" relation.