Inheritance in Kotlin

Vefa Can Beytorun
3 min readJan 1, 2023

--

Inheritance

Introduction

Hello dear Kotlin users, welcome to my first article. I have been wanting to write an article for a while and I didn’t know which topic to choose. I chose inheritance, which is the topic I havee been repeating over and over, because I thought that if I put what I understood in writing, I could reinforce it better. I hope it will be of use to you and help you understand. Let’s go to our article.

Inheritance

Inheritance is the analogy of a class to a superclass in programming. In other words, it means that we derive another class from a class that we have. First, the open keyword is added to the beginning of the base class. Then, the name of the base class is written with the “:” operator at the end of the class we want to simulate and its variables are added inside.

To illustrate with an example,

First, let’s create a class called Person and write the keyword open at the beginning, then add the name and age variables.

Then let’s open two different classes and connect them to the Person class with the “:” operator. As you can see, we have given the same variables by binding them to the Person class, since both the student and the teacher have a name and age.

Then, in the main function, let’s bind our Student and Teacher classes to an object and call the functions we wrote inside their classes.

The output we will see when we run the function is as follows:

What if we wanted to show the characteristics of these people separately?

For example, reflection sounds made by animals, cats and dogs cant make the same sound… If we have separate classes for cat and dog and we have linked these classes to a superclass, how can we encode it? Let’s show it on the example above.

For example, let’s assign the department of our student and the branch of our teacher as variables.

Above, our student and teacher, the person depends on our class, but both of our classes have their own characteristics.

What might confuse us here is that the init block is called when the object from the init block class is created and the code block inside is executed.

The output we will see when we run the function is as follows:

Override

In inheritance, the subclass can change the data it receives from the superclass and overwrite it with another value. For example, children usually always say 1 more than their age. When we create a Child class and connect this class to the Person class, we can print the mourning function from the Person class with the keyword override for 1 year older.

Here, when we enter the value 7 into the function, we see the value 8 in the output:

Conclusion

As a result, we can link many classes together in inheritance and add unique features to each of them, but it is important to remember that each subclass must have the feature of the superclass. Traits passed from superclasses to subclasses can be overridden, modified, and turned into a subclass’s unique feature.

Thank you for reading and have a nice day.

--

--