skip to content

Interface Default Methods & Properties

Kotlin interfaces can have method bodies and abstract properties, but no backing fields, so state still lives in the implementing class. That state rule is exactly the point interviewers probe when comparing interfaces with abstract classes.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

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

open as a page

How do properties work in Kotlin interfaces? Explain abstract properties (no backing field) and properties with default accessors.

level: middleimportance: must knowfreq 60%

basics

~10 s

An interface property has no stored value—no backing field. It can be abstract (the class must provide it) or it can define a getter that computes a value from other members.

open as a page

Kotlin allows a class to implement multiple interfaces but extend only one class. How do interfaces act as multiple-supertype contracts, and what does the type see?

level: middleimportance: should knowfreq 50%

basics

~20 s

A class can list several interfaces after the colon, separated by commas, and must satisfy all of them. It then counts as each of those types, so it can be passed wherever any one of them is expected.

open as a page

Now that Kotlin interfaces can have default method bodies and property accessors, when would you still choose an abstract class over an interface?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Use an interface when you only need behavior contracts and want a type to implement several. Use an abstract class when you need stored state, a constructor, or initialization logic, since interfaces can't hold any of those.

open as a page

What are the limits on what an interface default method or accessor can do regarding state, and how do teams work around the lack of interface fields?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

Default methods can't store or remember anything because interfaces have no fields. They can only use parameters and call other interface members. To keep state, implementers add their own property, which the interface declares as an abstract property.

open as a page