Interface vs abstract class

Runtime, build and Gradle Junior Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Predict the output

What does this print?

interface Greeter { fun greet() = "hi" }
abstract class Named(val name: String)
class Bot : Named("bot"), Greeter

fun main() {
    val b = Bot()
    println("${b.greet()} ${b.name}")
}
Options
Hint 1 of 1

Interfaces can have default bodies.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A hi bot

Correct. Bot gets the default greet() from the interface and name from the abstract class.

Why not the others?
  • B It doesn't compile. A class can extend one class and implement interfaces.
  • C bot hi. Check the order in the string.
  • D null bot. greet() has a default body.

30-second answer

An interface declares a contract and can have default method bodies and abstract properties, but no state; a class can implement many. An abstract class can hold state and constructor parameters, but a class extends only one. Use interfaces for capabilities and seams for testing; use an abstract class when implementations share real state or setup.

Practise saying it · 25 s. No recording, just a timer

Explain interface vs abstract class.

Say your answer out loud, then check which of these points you covered:

  • Interface: contract, no state, many
  • Abstract: state, one parent
  • Interfaces for seams
  • Sealed interfaces

Deep dive

Kotlin interface properties can't have backing fields.

Since Kotlin 2.2, interface methods with bodies compile to real Java default methods by default (the -jvm-default option, which replaced -Xjvm-default).

Sealed interfaces model closed sets without forcing one parent class.

Abstract classes can have protected members.

Common mistakes

  • Abstract base classes just to share one helper function.
  • Interfaces with dozens of methods.
  • Thinking interfaces can't have method bodies.

Senior take & follow-ups

Contract vs shared state; one inheritance vs many.

Follow-up questions

Think of your answer first, then open the hint.

  1. Can a Kotlin interface have a property?

    Show hint

    Yes, abstract or with a getter, but no stored state.

  2. Sealed interface vs sealed class?

    Show hint

    Sealed interface lets subtypes extend other classes too.

Suggest a fix if something here is wrong or out of date.