TExcelTAD
Home › Kotlin › Kotlin Abstract Classes & Interfaces

Kotlin Abstract Classes and Interfaces

Sometimes a parent class describes a general idea that shouldn't exist on its own — there is no "generic shape", only circles, squares and triangles. Abstract classes and interfaces let you describe what subclasses must provide without saying how.

Abstract classes

An abstract class can't be instantiated. It may declare abstract members (no body) that every concrete subclass must implement, alongside normal members with code. Abstract classes and members are automatically open.

kotlin · try it yourself
abstract class Shape(val name: String) {
    abstract fun area(): Double

    fun describe() = "$name with area ${"%.2f".format(area())}"
}

class Circle(val r: Double) : Shape("Circle") {
    override fun area() = Math.PI * r * r
}

class Rect(val w: Double, val h: Double) : Shape("Rectangle") {
    override fun area() = w * h
}

fun main() {
    val shapes: List<Shape> = listOf(Circle(1.5), Rect(2.0, 3.5))
    for (s in shapes) println(s.describe())
    println("Total area: ${"%.2f".format(shapes.sumOf { it.area() })}")
}
Circle with area 7.07
Rectangle with area 7.00
Total area: 14.07

Trying to create a Shape directly fails:

kotlin · try it yourself
abstract class Shape {
    abstract fun area(): Double
}

fun main() {
    val s = Shape()
}
File.kt:6:13: error: Cannot create an instance of an abstract class.

Interfaces

An interface is a contract: a set of functions and properties a class promises to provide. Interfaces can contain default implementations, but they can't store state (no backing fields). A class can implement many interfaces.

kotlin · try it yourself
interface Drawable {
    fun draw(): String
}

interface Clickable {
    fun click(): String
    fun showTooltip() = "Click me!"     // default implementation
}

class Button(val label: String) : Drawable, Clickable {
    override fun draw() = "[ $label ]"
    override fun click() = "$label was clicked"
}

fun main() {
    val b = Button("OK")
    println(b.draw())
    println(b.click())
    println(b.showTooltip())
}
[ OK ]
OK was clicked
Click me!

Note the syntax: interfaces are listed after : without parentheses, because they have no constructor. A superclass is written with parentheses: class A : Base(), Drawable.

Interface properties

An interface may declare properties. Implementing classes provide them, often directly in the constructor:

kotlin · try it yourself
interface Named {
    val name: String
    val greeting: String get() = "Hello, $name"
}

class Robot(override val name: String) : Named

fun main() {
    println(Robot("R2").greeting)
}
Hello, R2

Programming to an interface

Functions that accept an interface type work with every implementation — current and future:

kotlin · try it yourself
interface PaymentMethod {
    fun pay(amount: Double): String
}

class Card(val last4: String) : PaymentMethod {
    override fun pay(amount: Double) = "Paid $amount with card *$last4"
}

class Wallet(val email: String) : PaymentMethod {
    override fun pay(amount: Double) = "Paid $amount via wallet $email"
}

fun checkout(total: Double, method: PaymentMethod) = println(method.pay(total))

fun main() {
    checkout(19.9, Card("4242"))
    checkout(5.0, Wallet("kai@example.com"))
}
Paid 19.9 with card *4242
Paid 5.0 via wallet kai@example.com

Abstract class or interface?

Abstract class Interface
Can hold state (fields) yes no
Constructor yes no
A class can extend… only one many
Default method bodies yes yes
Typical use shared base implementation capability / contract (Comparable, Drawable)

Tip: Start with an interface. Use an abstract class when implementations genuinely share state or constructor logic.

Functional (SAM) interfaces

An interface with a single abstract function can be declared fun interface. You can then create instances with a lambda:

kotlin
fun interface Validator { fun isValid(s: String): Boolean }
val notEmpty = Validator { it.isNotEmpty() }

Test yourself

Can you create an instance of an abstract class directly?

  1. Yes
  2. No
  3. Only with new
  4. Only if it has no abstract members

How many interfaces can a Kotlin class implement?

  1. One
  2. Two
  3. Any number
  4. None

What can an interface NOT have?

  1. Functions with default bodies
  2. Abstract properties
  3. Stored state (backing fields)
  4. Properties with custom getters