Big challenge in Kotlin : Interface vs Abstract class

Everybody who has passion about the programming , definitely encountered Object Oriented Programming principles. Actually, if anyone work with one programming language which is object oriented , and wants to apply all of that principles faced four milestones of Object Oriented Languages. These are encapsulation ,inheritance, polymorphism and abstraction. Abstraction can be most central principle. So, you can ask what is the abstraction? I would like to explain it with real life example.

This example will be loved especially by coffee addicted programmers.But, sorry about my mistake! I forget that every programmers love the coffee. Anyway, let’s begin!

Let’s image that you woke up in morning and left your home to go your office. And, you don’t have time so much. While , you are walking around the street , you wants to drink coffee and meet coffee vending machine.Now our story is begin! Firstly, you selected your coffee type.(Latte or americano).After that, you wanted to add some milk and sugar. You just pressed the buttons to do that.And after a while, the machine served the your coffee.Your coffee is ready! You can drink it now. I don’t know , you reliaze that we don’t care how to coffee was prepared by the coffee vending machine. We can just use coffee vending machine interface and the machine prepared the coffee for us. The anyone who wants to get the coffee don’t have to how is going on the vending machine while preparing the coffee. That is the point ! One software can be created with the same concept. You can ask how? Absolutely, with Object Oriented Programming benefits.

Abstraction is process which provide to hiding every implementation information from the user. We can provide abstraction with interface and abstract classes in Kotlin programming language. I would like to cut in object oriented entrance and keep going with firstly interfaces in Kotlin. Let’s have a look step by step.

  1. Interfaces have abstract methods in Kotlin

If you are familiar with Java before, you already know that information.In Kotlin, interfaces can have abstract method declarations as well. In below, any class which implemented CoffeeMachineController class , doesn’t have to provide any body for this interface methods.

interface CoffeeMachineController {

fun addMilk(){
Log.e("TAG","Milk is adding")
}

fun addCoffee(){
Log.e("TAG","Coffee is adding")
}

}

2. Interfaces can have properties

Yep, you have heard right. Interfaces can have properties but these properties need to be abstract.

interface CoffeeMachineController {

var coffeeAmount: Int?

You saw the error code in above. If you don’t want to make abstract any value, you can assign specific value and access the property implementation with accessors. It is possible only with accessor. It is the another way to access properties in interfaces. Example code pieces is in below about properties implementation.

interface CoffeeMachineController {

var coffeeAmount: Int?

val milkAmount: Int?
get() = 5


fun addCoffee() {
Log.e("TAG", "Coffee is adding")
coffeeAmount = 5
}

fun addMilk() {
Log.e("TAG", "Coffee is adding")
}

}

3. One interface can implement another interface

This title means ‘interface inheritance’ actually. One interface can derive from another different interface. This derived interface requires to implement abstract methods and properties like class. If it doesn’t implement them, the class which implement derived interface, have to implement these abstract methods and properties. Please check the example in below.

interface CoffeeTypeController {
val coffeeType: String
fun displaySelectedCoffeeType()
}


interface CoffeeMachineController : CoffeeTypeController {
override val coffeeType: String
get() = "Espresso"

override fun displaySelectedCoffeeType() {
Log.e("LOG", coffeeType)
}
}

class CoffeeMachine() : CoffeeMachineController {

}

Everything is normal in this code(above). This code can work well properly. As I mentioned before, if you remove CoffeeMachineController interface body, CoffeeMachine class needs to implement CoffeeTypeController interface abstract method and property. This case showed in below code piece.

interface CoffeeTypeController {
val coffeeType: String
fun displaySelectedCoffeeType()
}


interface CoffeeMachineController : CoffeeTypeController {}

class CoffeeMachine() : CoffeeMachineController {
override val coffeeType: String
get() = "Espresso"

override fun displaySelectedCoffeeType() {
Log.e("LOG", coffeeType)
}
}

Until this point, I mentioned interfaces with three essential points. After this point, I would like to talk about Abstract Classes even if it is too much as Interfaces.

The most important point about abstract classes, they can not be instantiated. That means, you can not create an object from abstract classes. If you wanna use abstract properties and methods of one abstract, you can derived a class from abstract class and create an object. It is actually polymorphism property in object oriented as well. I would like to carry on with piece of example in below.

abstract class CoffeeMachineController {

abstract var coffeeAmount: Int?

open val milkAmount: Int? = 5

}

In abstract class, properties needs to be initialized or abstract. If you want to use abstract keyword with properties, you must have abstract class . Property with initial value cannot be abstract. It against to abstraction fundamental of object oriented programming. Another keyword which is coming new with Kotlin is open. In kotlin, abstract classes properties are accepted private as a default. So, if you don’t use open keyword properties which want to override in subclass, you can’t override them.

abstract class CoffeeMachineController {

abstract val coffeeAmount: Int?

open val milkAmount: Int? = 5

}

class DefaultCoffeeMachineController : CoffeeMachineController() {

override var coffeeAmount: Int?
get() = TODO("not implemented")
set(value) {}

override val milkAmount: Int?
get() = 3
}

You can override properly milkAmount property with open keyword. Moreover, if you are using abstract keyword with one of your class properties, you definitely override it. Otherwise, you will get the error.

Abstract classes also used for architectural works for class hierarchies. Let’s imagine that you need many fragment in an application. You can specify one base abstract fragment which is derived from Fragment class, after you can you it.

In end of my article, I would like to sum up important thing about abstract classes and interfaces. Abstract class can hold state but interfaces can not, like any object oriented programming language. Because, interfaces meant to provide abstraction. You can ask what is the of an object. After some research, I become aware of that state is what objects have. I hope that the abstraction puzzle is done :)

Thank you for your reading time :)

Android Developer