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.
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:
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.
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:
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:
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:
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?
- Yes
- No
- Only with
new - Only if it has no abstract members
How many interfaces can a Kotlin class implement?
- One
- Two
- Any number
- None
What can an interface NOT have?
- Functions with default bodies
- Abstract properties
- Stored state (backing fields)
- Properties with custom getters