7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.7 Inheritance

Hello, in this lesson, we will be learning about the concept of Inheritance in Kotlin. It will just be an introduction to the object-oriented concept that we have here. So let's get started here with Inheritance. By default, the classes in Kotlin are of public and final type, which means that we cannot inherit the properties of any classes in Kotlin. So to use the property of a class or to implement inheritance in any class, we need to make the class open for inheritance. To make the class open for inheritance, the keyword open is used. We will see how do we use it in a few. So what happens in inheritance is, the child class or the derived class inherits the property from the parent or the base class. Which means if we have some functions or field variables in the parent class and we inherit this parent class in some other class, the child or the derived class can use those properties of the parent class. I'll show you a demo in a few. Thus, the child object acquires all the properties of the parent object. Let's move to the IBE and let me show you what am I talking about. So here in my Kotlin file, I had to find two classes. One is Dog and the other is Cat. In the Dog class, I have two field variables, color and breed and two functions, bark and eat. Whereas in the Cat class, I have two field variables color and age and two functions meow and eat. Looking at this carefully we can see that the field variables, color, is present in both the classes, Dog and Cat, and the eat function is present in both the classes, in Dog as well as Cat. So what we can do is we can define a parent class, let's say animal, and define this color field variable and the eat function there. And then we can inherit that class in our Dog and Cat class so that we can use its property in our classes. Let me show you what am I talking about. So here is my animal class which has the field variable of color and the function eat. Let me print the output here as Eats. By default, all the classes in Kotlin are public and final, which implies that we cannot inherit the class Animal to any other classes. So, to inherit the class Animal in our other classes, we need to make it open. Now when this Animal class is open, we can inherit it in our Dog and Cat class. So to do that, rewrite the printIn class name in the class header following the colon. Here we have an error which says, this type has a constructor and must be initialized here. So we will initialize its constructor. And same way, we will inherit the Animal class in our Cat class. And we will remove this color and this color from both the classes. As well as we can remove the Eat function from both our classes. Thus, Animal is the parent class or the base class. Whereas, Dog and Cat are child class or the derived class. Now how can we use these properties of Animal class from the Dog class? Let's see how can we do that. In our main function, let us create the object of the Dog class. And now using this Dog object, we can call the eat function of the Animal class. And similarly using this Dog object, we can call the eat function of the Animal class same way we call the bark function of the Dog class. So let us call the eat and bark function and see the output. We can see in the suggestions, as well with the dog object, we get the options for breed, bark, which belongs to the Dog class. And then we have color and eat which belongs to the Animal class. So we can see that color and eat are the properties of Dog class as well. So this way using the dog object, we can call the function of the Animal class which we have inherited. And then we have the function of the Dog class itself. And then we can initialize the field variables of the Dog class as well as the variables of the Animal class. Similarly, we can also do for the Cat class. So this way, we have integrated the property of Animal class into our type classes. Now there is one more important thing. This Animal class or the parent class inherits the property of another parent class, which is known as Any. This Any class is present by default in Kotlin. Thus, all the parent classes derive its properties from this Any class. Thus we saw how inheritance makes our code concise. It help in code reusability, which we have just seen. And method overriding, which we will see in the other courses. Digging a little deeper into inheritance, there are three types of Inheritance. Single Inheritance, Multilevel Inheritance, and Hierarchical Inheritance. What we saw was the Hierarchical Inheritance, in which our Dog class and the Cat class, which is represented here as X and Y, inherited a property from a single class which was the Animal class. And this Animal class inherited its property from the Any class. Thus, we just saw the example of the Hierarchical Inheritance. The other two type of inheritance are Single and Multilevel Inheritance. In Single Inheritance, there is just a single class which inherits its property from the parent class. And again, the parent class inherits its property from Any class. X will be the time child and Y will be the parent class. In Multilevel inheritance, there are multiple level of inheritance. Let's say there are three classes, X, Y, and Z, in which X will inherit the property of Y. Thus X can use all objects of Y Class and Y will inherit the property of Z. Thus Y can use all the property of Z Class. So we can see X Class can use the properties of Y as well as a Z Class, but Y can use only the property of Z Class. Thus, X will be the child class for which the parent class is Y. Whereas, Z is the parent class of Y. But since Y inherits a property from Z Class and X inherits Y, so Y will be child class for Z and parent class for X. And again, like all of the parent classes, Class Z will have its property from Any class. So this was all about Inheritance in Kotlin. In the next lesson, we will be learning about null pointed exception in Kotlin. How Kotlin manages the null pointed exception and how it makes our programming safe. Stay tuned and keep smiling.

Back to the top