Kotlin: why use Abstract classes (vs. interfaces)?

KotlinAbstract Class

Kotlin Problem Overview


I'm aware of two differences between Abstract classes and Interfaces in Kotlin:

  • An abstract class can have state (e.g. var...)
  • A class can implement multiple interfaces, but not multiple abstract classes.

Since Kotlin is a rather fresh language, I wonder why Abstract Classes were not abandoned? Interfaces seem superior tool, with a very little need for Abstract Classes.

To elaborate: Kotlin does support concrete function implementation in interfaces, e.g.:

interface Shiny {

    fun shine(amount : Int)  // abstract function

    fun reflect(s : String) { print ("**$s**") }  // concrete function

}

Can someone provide a strong practical example of the need for Abstract Classes?

Kotlin Solutions


Solution 1 - Kotlin

The practical side of abstract classes is that you can encapsulate a part of implementation that works with the state, so that it cannot be overridden in the derived classes.

In an interface, you can only define a property without a backing field, and an implementation class must override that property (with either a backing field or custom accessors).

Given that, you cannot define logic that stores some state in an interface in a reliable way: an implementation class might override the properties in an unexpected way.

Example:

interface MyContainer {
    var size: Int

    fun add(item: MyItem) { 
        // ...
        size = size + 1
    }
}

Here, we provide a default implementation for add that increments size. But it might break if an implementing class is defined like this:

class MyContainerImpl : MyContainer {
    override val size: Int 
        get() = 0
        set(value) { println("Just ignoring the $value") }
}

On contrary, abstract classes support this use case and thus allow you to provide some guarantees and contract for all their implementations: they can define some state and its transitions that will stay the same in a derived class.

Apart from that, abstract classes can have non-public API (internal, protected) and final members, whereas interfaces cannot (they can only have private members, which can be used in the default implementations), and all their default implementations can be overridden in the classes.

Solution 2 - Kotlin

Abstract classes exist essentially for a hierarchy of classes. For example, if the abstract parent class had a concrete function that was also defined in the child class which extends the parent class, then in certain cases it would be necessary to call the parent's function. When you use an interface it is impossible to do so due to the entirely abstract nature of the class.

Solution 3 - Kotlin

one of the difference is , if you make it as interface we can inherit multiple interfaces to a class but for a abstract class only single abstract class can be inherit to a class ( only one class appear in a supertype list)

happy coding !!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLior Bar-OnView Question on Stackoverflow
Solution 1 - KotlinhotkeyView Answer on Stackoverflow
Solution 2 - KotlinjavajunkeeView Answer on Stackoverflow
Solution 3 - KotlinMouliView Answer on Stackoverflow