skip to content

In Kotlin, can an interface provide a default implementation for a method? Show how and explain what a class gets when it implements that interface.

level: juniorimportance: must knowfreq 70%

answer

  1. Body in interface = default implementation
  2. Abstract member = no body = must override
  3. Interfaces hold no state (no backing fields)
  4. Compiles to Java 8 `default` method on JVM
  5. Members are open; override is optional for defaults

basics

~10 s

Yes. A Kotlin interface method can include a body. Classes that implement the interface inherit that body for free and only need to override it if they want different behavior.

solid answer

~40 s

Kotlin interfaces can declare methods with a body, giving a default implementation. Any class declared with `: MyInterface` automatically inherits that body without writing anything. The class only writes `override fun ...` when it needs to change the behavior. This differs from abstract methods (no body) which a non-abstract class MUST override. Default methods let you add behavior to a contract without forcing every implementer to copy it. Under the hood the Kotlin compiler emits a real Java 8 `default` method (no separate `DefaultImpls` helper class on modern JVM targets), so Java callers see it too. You cannot store state: interfaces have no constructor and no backing fields, so a default method can only call other interface members or use its parameters.

code

kotlin · 13 lines
kotlin
interface Logger {
    fun tag(): String
    fun log(msg: String) = println("[${tag()}] $msg")  // default
}

class FileLogger : Logger {
    override fun tag() = "FILE"
    // log() inherited, no code needed
}

fun main() {
    FileLogger().log("started")  // [FILE] started
}

go deeper

for a junior

Knows an interface method can have a body and that implementers inherit it without rewriting it.

for a middle

Distinguishes abstract vs default members, knows override is optional for defaults, and that interfaces are stateless.

for a senior

Explains the Java 8 default bytecode emission, interop implications, and the 'abstract primitives + default conveniences' design.

for a principal

Discusses API evolution/binary-compatibility tradeoffs of adding default methods to a published interface and JVM-target nuances.

## What a default method is A Kotlin `interface` may declare a function with a **body**. That body is the *default implementation*. Compare the two forms: ```kotlin interface Greeter { fun name(): String // abstract: no body, must be implemented fun greet(): String = "Hi, " + name() // default: has a body } class English : Greeter { override fun name() = "Sam" // required (abstract) // greet() inherited for free } English().greet() // "Hi, Sam" ``` ## Rules to remember - A member **with `= ...` or `{ ... }`** is a default method; a member with **no body** is abstract and a concrete class must `override` it. - You override a default method exactly like any other: `override fun greet() = ...`. Overriding is optional for default methods, mandatory for abstract ones. - Interfaces have **no state**: no constructor, no `init` block, no backing fields. So a default method can only use its parameters and call other interface members (abstract or default). It cannot read or write an instance field. - Interface members are `public` and `open` by default; you cannot mark an interface member `final`, so a default method can always be overridden. ## How it compiles (JVM) On JVM targets that support it, Kotlin emits a real `default` method on the interface bytecode (the Java 8 mechanism). That means a Java class implementing the interface also inherits the default. This is why interface default methods interoperate cleanly with Java. ## When to use Use a default method to evolve a contract without breaking existing implementers, or to provide convenience methods built on top of a small set of abstract members (the classic "a few abstract primitives + many default conveniences" design, like `Comparable` or collection-style interfaces).

  • Can a default method read a property declared in the interface?
    It can read a property declared in the interface, but only via its accessor (getter). Since interfaces have no backing field, the implementing class supplies the actual value/getter.
  • Is overriding a default method required?
    No. Overriding is optional for default methods and required only for abstract members that have no body.

An interface default method is like a recipe printed on the box: you can cook it as-is, or swap in your own version.

saying these in an interview costs you the question

  • Saying Kotlin interfaces can never have method bodies (confusing with old Java)
  • Claiming a class must override a default method
  • Thinking a default method can store/mutate an interface field
  • Believing default methods are private by default

context