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

Abstract Classes in OOP

Start course
1h 31m

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.


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.


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


Well, if you're here and I'm here, we must be in the next video. Hello, my friends. So, right now I want to talk about the concept of this abstract class. Alright. So, I would say that the most important feature that will distinguish abstract classes from other, Kotlin classes, is that we wouldn't be able to create any objects using abstract classes. Now, sometimes according to the project that we developed, we might not want to create objects from classes, but we might want the functions or properties defined in these classes to be used. See where I'm going? So, in that kind of case and you might not be able to speculate yet where you would use it, but we would create the abstract class.

Alright, so to create an abstract class, you just got to use the keyword 'Abstract' before the class prefect. But also to create an abstract function or property, we have got to use the abstract keyword before declaration word. Such as fun, var, val. So, why don't we just open up Android Studio and get some practice in. So, you know what to do. Follow along with me, if you're not already there. So, create a new package. Right click on the 'Project Package Folder', choose 'New Package' option. And we'll let the name of this package be abstract. So, now I want to create a superclass and a subclass in this package. Right click on the package, select 'New', 'Kotlin Class/File' and here, the 'Class' will be selected. So, I will set the name of this Class to be Vehicle. So, Vehicle class will be superclass. We'll also make this class abstract. For this, I'll just write, abstract is my keyword before the class statement.

So, now let's define some properties and functions within this class. Now, some of these functions and properties will be abstract. So, first we'll create a function called vehicleName. This function should take a parameter and that parameter is name, type is String. It also returns a value of type String. Now, note that I'm not creating the body of the function. So, I didn't specify the scope of this function with curly braces. So, that's why we're getting this warning. So, we've got to either make this function abstract or create this body. And because this is the time when we're focusing on abstract, we'll just make this function abstract. So again, I will use the keyword abstract before the fun statement, and as you can see, the warning has now vanished.

Alright, so let's define another function. We'll let the name of this function be vehicleType. Now, this function should also take a parameter named type:String. Also, this function here returns the value of the type String. Now, this time I'm not going to make this function abstract. So, I'm going to create the body of this function. You follow? So, here I'll just write return type, and this function will return the type of vehicle. So, as you can see, we may recognize functions as abstract or non abstract but what's really the difference between these two functions, we'll get to that in a minute. Right now, we're going to define two properties. And again, let's define these variables as abstract and non abstract.

First of all, define a property named model and type String. If I leave it that way, Android Studio does give us a warning. So, in other words, I've got to abstract or initialized this variable. So, I'm going to make this variable abstract. I write the keyword abstract before the var statement. But now let's create another property. I will create a variable named speed of the type Int. This time I will initialize this variable, but I'm going to initialize it as null. So, I can set the value of this variable later. Alright, my friends, so as you can see we've got two functions, two features that we've created here. The function that we created as an abstract has nobody and we did not initialize a variable.

Now, with that hanging in the balance, let's create a Kotlin file now. Alright, click on the 'Abstract Package', choose 'New', 'Kotlin Class/File' options, file type will be File, I also set the name of this file to be AbstractTest. So, now we've got to create our main method, and we will try to create an object from the vehicle class that we just created. So, here I'll write, var vehicle equals Vehicle, and notice here that I've selected the Vehicle class in the abstract package here. As you can see, Android Studio warns us. If we hover over to this warning, the code editor tells us that the vehicle class is an abstract class and we cannot create an object. But remember what I said the beginning of the lesson, the biggest feature of abstract classes is that we cannot create objects from these classes. Those are the rules. Might seem a little pointless then.

Or maybe it's confusing. But when you work on really large projects in the future, you can create multiple classes, and you may want some functions or properties to be used in all classes. So, that's the time that you can create all common functions and properties under an abstract class. In addition to all that, you can create this class only to use common functions, and you can prevent the creation of objects from this class. You'll understand why, shortly. But first, I want to show you that we will create a subclass. So, now I'll just right click on the 'Abstract Package', choose 'New', 'Kotlin Class/File' option and its type is going to be Class. I'll set the name of this class to be Car. This class will inherit from the Vehicle class. I write Vehicle after the colon, and what you see here is Android Studio throws up a warning because there is one property and one function that we've defined as abstract in the Vehicle class.

So, we need to implement this feature and function here. So, from here I select, 'Implement as constructor parameters' as my option. And from here, I select the model  variable Press the 'OK' button, and as you can see, we override the property named model which we created as abstract in the Vehicle class. So, it's a constructor here. So, now let's implement the function. I select the 'Implement members', select 'vehicleName(name: String): String', and press 'OK'. So, as you can see, we have now overridden the function named vehicleName that we've created as an abstract in the Vehicle class. I want you to notice, we do not need to override the functions and the features that we define as non-abstract. It might still be a little abstract, but that's the functions or variables of this abstract class and how to use them.

So, why don't we just return, we'll write a return name here, and this function will return the vehicle name, and we'll create an object from the Car class In the main methods; var car equals Car. Now, we must write the model parameter in princes, so I'm writing 2021 here. And on the bottom line when we type car. we can access all the functions and features that we defined in the Vehicle class. So, first I'll choose speed. So, we'll let speed be 300.

Now, let's print all the properties to the console. I'll type out and press 'enter'. I will write, Name colon in princes and write the name of the vehicle. I'll open up a curly bracket after the dollar sign, and write the car.vehicleName function here. I'll write Ferrari is a parameter inside the parentheses. I will not create a separate print in method for each feature. I can define them all in this method. So, for this, I'll write \n. So, this syntax is used in Spring expressions to jump to the next line. So, that is on the console screen, the phrases after \n are displayed on a bottom line.

So, now let's write the type of the vehicle. I'm writing, Type : ${car.vehicleType}. I also write Car as a parameter, since it does not fit on the screen, let's write it on the bottom line. And then by pressing 'Enter', I'll go to the next line. And again I type \n and press 'Enter' to switch to the next line. Now, let's write the model of the vehicle and I'm writing Model : ${car.model} and I type again back \n. And again, by pressing 'Enter', I'll go to the next line, and then finally we'll write the speed of the vehicle. So, I'm writing Speed : ${car.speed}. Now, let's run our code. So, as you can see, we've printed the vehicle's name, type, model and speed to the console. Alright, my friends, that is abstract classes in a whirlwind. We're going to take a short break, don't worry about it. And the next lesson, we're going to go through the interface. So, I'll see you then.


About the Author
Learning Paths

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