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

Viewed 15466

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?

3 Answers

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 !!!

Related