Explore interfaces in Kotlin, which are similar to those in Java with a few extras worth learning. The exploration includes contrasting interfaces and abstract classes.
- [Instructor] Kotlin's interfaces are similar to those in Java. They both contain declarations of method implementations which is also similar to abstract classes. But unlike abstract classes, interfaces can't store state. They mainly state what method must be defined by any users of the interface and prefer to put interfaces in their own file. So let's create one by right clicking on com.tekadept.demo in the project window, choose new, Kotlin File/Class.
We're going to name this Vehicle. And we're going to import our package in. And then, we'll create our interface. So our interface is going to be called Vehicle. We're going to have a open and close curly brace. And then inside we define all of the things that we want everybody that implement this interface to have.
So we'll create a property and that property is going to be called MakeName. We'll also create a function which we're going to call go. And we'll just have it say something fine like ... I'll also have a function stop and stop will also just have a message.
And let's see one more, getMpg and mpg is going to return an integer. And in this case, we'll have a return 50. And finally, we will have ... a function which we don't give an implementation which is going to be getDoors, and it will return an integer.
Okay, so, any method in the interface that has a function body will be inherited by any implementers of the interface. If the method doesn't have a body, like in getDoors, then it must be implemented when the class implements an interface that must override all the properties with implementations in all methods that lack bodies. So, let's go ahead and create somebody that uses our Vehicle interface. And we will create another Kotlin class. And this time, we'll call it Car.
So again, what I did, was I right click on com.tekadept.demo and chose new and then new Kotlin File or Class. We got a whole new Car, and we'll import the package. And class, car, and we say colon. And the colon indicates that car implements ...
And in this case, it's going to be the vehicle interface. We're going to have an open and close curly brace. Now right now, the editor is giving me red marks which basically says," Hey I haven't implemented everything." So we're going to go ahead and say override val MakeName. All of the properties must be implemented in any class that implements this interface.
So in this case, we'll call MakeName and we just say Ford, then the other thing we must override is getDoors cause it did not have an implementation. So we say, override function getDoors. That, it is going to return an Int. And let's just say, return five. You know, this red line is now gone. Meaning that I've implemented other things that we have to do in a class.
And now, if we go back over to our main file, we can go ahead and create an instance of this class. So we'll say val myCar equals, Car. So this will generate an instance of the class. And we'll say, myCar.go and that should print a message out. And so if we run the code, we can see where it says, "Vroom, vroom." Let's just do one more to see if everything is working.
We'll say, println, my car's make equals $ sign and we'll say myCar.MakeName and when we run this, we see that we get my car's make name equals Ford. Here, we've created an instance of the class and used the go method which inherited from the Vehicle interface.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin