What is a motivation to make classes final by default in Kotlin? Do you agree with that decision?

Technology CommunityCategory: KotlinWhat is a motivation to make classes final by default in Kotlin? Do you agree with that decision?
VietMX Staff asked 3 years ago
  • First Kotlin takes many ideas from the functional programming world and uses immutability as often as it can to avoid all the known problems with mutation. Also proper designing a class for inheritance requires an excruciating amount of work (and building at least 3 separate subclasses, to verify that you base class actually is useful). Most classes should be final. Extending them is probably a bad idea.
  • The second thought which comes to my mind is that inheritance is often missused. There is the principle “Favor composition over inheritance” as a guideline for better designs. So declaring every class as final by default forces the developer to at least stop for a moment and think about alternative ways to solve the problem instead of using inheritance for the wrong reasons.