1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Object Oriented Programming (OOP) in Kotlin

Inheritance in OOP

Start course
Overview
Difficulty
Intermediate
Duration
1h 31m
Students
11
Ratings
5/5
starstarstarstarstar
Description

This course covers the concept of Object-Oriented Programming in Kotlin, which is a method of designing and implementing software. It simplifies software development and maintenance by providing concepts such as object, class, inheritance, polymorphism, abstraction, and encapsulation. This course will explore those.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Resources

Course GitHub repo: https://github.com/OakAcademy/Kotlin-Programming-Course/tree/main/ObjectOrientedProgramming

Transcript

Hello everyone. So, you know what? You've been chomping at the bit. I know it. You want to learn about inheritance. So, inheritance is an absolute fundamental concept of object oriented programming. So, it's used when we have and is a relationship between objects. So, in Kotlin, all classes can inherit variables and methods from another class. An inheritance enables us to reuse existing code. I'll show you, in our example, we have this superclass Vehicle. Now, all vehicles have a type, model and a max speed. Also, all vehicles can start and stop.

All right, so we understand this to be the base class. Also, we have two classes here named Car and Motorcycle. Car and Motorcycle classes are subclasses of Vehicle. So, we use the colon sign : by the subclass to inherit the features of the superclass. So, the Car and Motorcycle classes inherit variables and methods from the Vehicle class. And there's no need to declare these variables or methods again for the Car and Motorcycle classes. The Car class has its own variable. This is named isAutomatic. The Motorcycle class has its own variable that's named motorType.

Now, finally, I want to mention this, in these Kotlin classes, so Kotlin classes are final by default. In fact, I'll put it in other way, the final modifier marks classes and methods as no way, not allowed to be overridden. Therefore, in order for subclasses to inherit from superclasses, the open statement must be written before the class statement in superclasses, you follow? It can be inherited from a superclass only by using the open statement. All right, so now that you know that, let's open it in Studio and get some practice in.

So, first I want to create a new class. I'll right click on the project package folder with the mouse, choose 'New Kotlin Class/File'. I'm specifying the name of this class Vehicle. So, let's start to write a program that prints the properties of sub-classes using inheritance. Vehicle class, that's my superclass. Let's declare three instance variables in the Vehicle class. We declare two variables that are named  type and model with string type and variable maxSpeed with int type. Now, let's declare void type method show() to display values of type, model, and maxSpeed variable. After this, we'll use the printIn method. I'll write sout and press 'Enter'. Inside the parentheses, I'll write ("Type : $type"), copy and paste this code twice. And then I'll write, ( "Model : $model"). And next up I'll write ( "Max Speed : $maxSpeed").

All right, so there we have, my superclass is now ready. So, let's start to declare sub-classes. Let's create a new subclass. Right click on the package, and select 'New Kotlin Class/File', specify class name, Car. Now, in order to implement inheritance, we have to add the ':' after the subclass Car. Our subclass Car inherits from superclass Vehicle. So, this means that subclass Car inherits the show method and the variables from superclass Vehicle. Right? Now, also, I'm sure you noticed Android Studio does issue a warning. And that's because as I was just trying to tell you in the beginning of the lesson, the Vehicle class is final by default. Also, since the empty constructor is also created by default, we've got to define it here as well. So, I'll show you how.

Let's select the 'Change to constructor invocation' option here, and as you can see, the warning still persists. You thought I was going to make it go away, but I do want to show you this, how to fix this error, we'll need to make the Vehicle class open, right? You remember I was telling you the theory. So, here, when you make the Vehicle class open, and by the way, you can also do this by clicking here, or in the Vehicle class you can fix this error by using the open keyword before the class statement. Of course, that's your choice. But, here it is, there it's gone. Also Car class, empty.

So, why don't we create another subclass? Right click on package and select 'New' 'Kotlin Class/File.' Specify class name, Motorcycle. Our subclass Motorcycle inherits from superclass Vehicle. So, this means that subclass Motorcycle inherits the show method and the variables from the superclass Vehicle. Also, the Motorcycle class is empty. So, now we can create a new Kotlin file for the test of this inheritance concept that I've been trying to convince you of. So, right click on the package, select 'New' 'Kotlin Class/File', select the 'File' option here, specify class name as InheritanceTest. We could also do this in the object class constructors file, but since we use that file a lot, I just want to show you through this new Kotlin file. And also, I'm going to create a main method.

So, here, now we can start to create a new Car object. So, I'll write var_car = Car(). And if you write car object and dot operator, then you will see the variables maxSpeed, model, and type, and show() method coming from superclass Vehicle. All right, so notice that we didn't create any variables or functions in a Car class. However, since the Car class inherits from the Vehicle class, we can access all of these properties and functions in the Vehicle class. So, we'll assign the Car  value to type variable. Ferrari value to model variable and value 320 to maxSpeed variable. And we'll call the show function from the superclass Vehicle.

So, let's run the code. And there you see the results in the console. Type is Car, Model : Ferrari, Max Speed : 320. All of these properties are inherited from superclass Vehicle. So, now I want to create a new Motorcycle object. So, I'll write var_motor = Motorcycle(). Now, when I write motor., you see the same properties and methods coming from the superclass Vehicle? So, let's assign Motorcycle value to type variable. Yamaha value to model variable and value 180 maxSpeed variable. And let's call the show method from superclass Vehicle.

All right, so now let's run the code, and there you got the results in the console. Type : Motorcycle, Model : Yamaha, Max Speed : 180. Put a helmet on. All right, my friends, so it's because of inheritance you can easily use the same properties of a class in more than one class without redefining them. So, there you go. That is inheritance. So, I'm glad we went through all that. We're going to take a short break here, and I'll see you in the next lesson.

 

About the Author
Students
85
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics