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

Interfaces 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, hello there my friends. So, I think we talked about abstract classes enough, right? All throughout the previous lesson and you mastered them. So, that means in this video we're going to learn the concept of interface and that means we're going to finish object-oriented programming. What? So, interfaces are custom types provided by COP. Okay, now these cannot be instantiated directly. Instead, these define a form of behavior that the implementing types have to follow. So, with the interface you can define a set of properties and methods that the concrete types must follow and implement. Now, interfaces are also abstract. So, in other words they have the characteristics of the abstract class that we learned in the previous video. So, we don't use interfaces to create objects.

Okay. The purpose of interfaces is this. In Kotlin, a class can inherit only one class. It cannot inherit from more than one class. So, in our projects, if a class needs to inherit from more than one class, well, we need to define these classes as drumroll please, interfaces. All right, so creating an interface is also quite simple. The interface definition in Kotlin begins with the interface keyword followed by, guess what? The name of the interface, followed by the curly braces within which the members of the interface resides. So, the difference is that the members will have no definition of their own.

All right. So, these definitions will be provided by the conforming types. So, that means that we should open up Android Studio and get some practice. So, here we're going to create a new package and I'll specify the name of this package. Call it interfaces. So now, let's create one class and two interfaces in this package. First, I want to create the class. All right, click on the interfaces package. Select 'New Kotlin Class File'. Its type will be class. I'll define the name of this class. I will not define any functions or properties inside this class. Of course, you could create it if you want.

Now, to move on, let's create the interfaces. So, right click on the interfaces package, select 'New Kotlin Class File'. Now, this time I'll choose 'Interface' from here and we'll let the name of this interface B can go. All right, so I'll press 'Enter'. Stay with me. So, as you can see, we've created the interface. And within this interface you can create functions or properties just like in abstract classes. So, first, let's create a function. I'm typing fun. Go here. Remember in abstract classes we had to either create the body of the function or write the abstract keyword at the beginning of the function, but interfaces don't need this. So, if you want to you can leave it that way and create the body of the function after the inheritance, or you should create the body of the function here.

So, let's create it here for now. So, we'll let this function print vehicles can go to the council. Let's create the variable and here I'll write val name:String. You cannot initialize the variables that you define in the interface. But I want to show you what happens if you try to initialize it because it's kind of fun. So, write equals Ferrari and look at that we're getting an error. Okay. Properties in interface cannot be initialized. You know that now if you didn't before. So what can you do? You can pass a value to variables using the get method that we learned a little bit earlier. So, I'll go to the next line by pressing 'Enter' and press the 'Tab' key. 

Now, I call the get method. Now, we can write Ferrari in here. And of course, if you notice we have defined this variable as val. So, we would still get an error if we define it as var. Why? Because you wouldn't be able to change the properties that you defined in the interface any time after you define it. Right. That's why I'm deleting this get method for now. All right, so let's create another interface. So, right click on the 'Interface Package'. So, like the new Kotlin class file and the type It will again be interface. So, said the name of the interface can stop.

Now, let's create a function in this interface. So, I'm typing in fun stop here. This time I will not create the body of this function. Now, let the vehicles class inherit from the interfaces that we created. First, let the vehicles class inherit from the interface named CanGo. As you can see the code editor warns us because there is one feature that we need to implement. So, from here we can select the implement as constructor parameters option, just as we did in the abstract classes, or by selecting the more actions option here, we can view other actions that we can take, or after clicking this warning with the mouse  if we press the 'Alt' and 'Enter' keys on the keyboard, we can view all the options that can be made. So, this time I'm just going to choose implement members option from here.

So, as you're seeing we override the property named name here. Also the get method has been created. So, if we created this get method inside the interface, we wouldn't need to override it here. Right. So, now let's just pass in a value to this variable here. I'll just type in Ferrari between double quotes. Great. So now, hold it make the vehicles class inherit from the can stop interface as well. So to do that, I'll just put a comment here and call the can stop interface. And as you can see a class cant inherit from more than one interface. If they can go and they can stop interfaces were classes, we would not be able to inherit from each. So, this is the most important feature of interfaces. Let's just pause for a moment and drink it all in.

Okay, because if you notice here, code editor will give a warning because there's a function that we need to implement. So, after clicking here I'll press 'Alt' and 'Enter' from the keyboard, I'll choose implement members as my option. And as you can see we override the stop method with its body. If you create the body of the function in interface, you don't need to implement it in this way. So, just like the start function and they can go interface, remember? Now, this function also prints Vehicles can stop to the console. So now, let's create a Kotlin file for the main method. I'll set the name of this file to be InterfacesTest.

Now, let's create the main method. Right? So first of all, we'll create an object from the vehicle class. All right, var vehicles + vehicles. Notice I'm choosing the vehicles class in the interfaces package. First, let's print the name of the vehicle on the console. I'll write vehicles.name, and we can even write this in the print in method directly. I'll type sout, press 'Enter', I write name, :, $, vehicle.name in parenthesis. Now, let's call the go and stop functions. So, here I am writing vehicles.go. Also write vehicles.stop on the next line. Now, let's run our code. And there you can see we have printed the expressions Ferrari vehicles can go and vehicles can stop right on the console. All right, my friends, that's how you use interfaces in Kotlin.

Now, boy, it kind of feels bittersweet, doesn't it? We've finished object-oriented programming. Also, we've come to the end of our course. So, I want you to go ahead, take a short break, let some of that trickle down or filter in or whatever you need to do. But in the next video, I want to take a look at what we've learned throughout the course because you're going to be amazed. All right. So, zoom out a little bit. All right, I'll see you in the next one.

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