1

On C++ or Python, there is support for multiple inheritance, so you can extend multiple classes or abstract classes. Thus, if you want to make something like an interface you just make a complete abstract class.

On languages like Java or Kotlin, there is no support for multiple inheritance, but instead they have interfaces which a class can implement multiple times to achieve something similar to this feature. However, on Kotlin or since Java 8 there is not much difference between abstract classes or interfaces since they both can have abstract methods and concrete implementations, so I wonder when to use each one and mainly what are the reasons the languages (in particular Kotlin which is more recent) are implemented in this way instead of using multiple inheritance if you could achieve the same thing with the latter one?

Or is just another way to achieve multiple inheritance or there is something that abstract classes along with interfaces provide that multiple inheritance do not and it influence in which cases you use one over another (interfaces over abstract classes)?

  • 2
    [A related question](https://stackoverflow.com/questions/2515477/why-is-there-no-multiple-inheritance-in-java-but-implementing-multiple-interfac) – Yoshikage Kira May 23 '21 at 04:02

1 Answers1

2

However, on Kotlin or since Java 8 there is not much difference between abstract classes or interfaces since they both can have abstract methods and concrete implementations

There is a major difference: abstract classes can have state (inherited by concrete implementations), while interfaces can't.

so I wonder when to use each one

You have to use abstract classes when you need to define stateful behaviour that you want to share with concrete implementations. Otherwise, it's better to stick to interfaces because it's less constraining: a class can implement multiple interfaces, and can implement interfaces by delegation etc.

what are the reasons the languages (in particular Kotlin which is more recent) are implemented in this way instead of using multiple inheritance if you could achieve the same thing with the latter one?

Just a guess, but Kotlin could probably not get multiple inheritance even if it were desirable, because the JVM doesn't support it. But anyway, I don't think this is a direction the designers want to take, because it would require solid use cases where this is really helpful. There is easy delegation with nice syntax sugar in Kotlin, which helps cover this sort of use cases in a more controlled manner.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • About your guess in the last paragraph: Python has multiple inheritance. That hasn’t stopped python from getting implemented on the JVM. – Nathan Hughes May 24 '21 at 17:44