Kotlin Interfaces

Defining contracts and shared behavior in Kotlin

🔌 What are Kotlin Interfaces?

Interfaces in Kotlin define contracts that classes must follow. They contain abstract methods and can have default implementations, allowing multiple inheritance of behavior while ensuring consistent structure across different classes.


// Simple interface example
interface Drawable {
    fun draw()
    fun getArea(): Double
}
                                    

Key Interface Concepts

📋

Abstract Methods

Methods without implementation

interface Animal {
    fun makeSound()
}
⚙️

Default Implementation

Methods with default behavior

interface Animal {
    fun sleep() = println("Sleeping...")
}
🏷️

Properties

Abstract and concrete properties

interface Named {
    val name: String
    val displayName: String
        get() = "Name: $name"
}
🔗

Multiple Inheritance

Implement multiple interfaces

class Dog : Animal, Named {
    override val name = "Buddy"
    override fun makeSound() = println("Woof!")
}

🔹 Basic Interface Declaration

Here's how to declare and implement a simple interface:

// Interface declaration
interface Vehicle {
    val maxSpeed: Int
    fun start()
    fun stop()
    
    // Default implementation
    fun honk() {
        println("Beep beep!")
    }
}

// Implementation
class Car(override val maxSpeed: Int) : Vehicle {
    override fun start() {
        println("Car engine started")
    }
    
    override fun stop() {
        println("Car stopped")
    }
}

// Usage
fun main() {
    val car = Car(120)
    car.start()
    car.honk()
    println("Max speed: ${car.maxSpeed} km/h")
}

Output:

Car engine started
Beep beep!
Max speed: 120 km/h

🔹 Interface with Properties

Interfaces can declare both abstract and concrete properties:

interface Shape {
    val area: Double
    val perimeter: Double
    val description: String
        get() = "A shape with area $area"
}

class Rectangle(val width: Double, val height: Double) : Shape {
    override val area: Double
        get() = width * height
    
    override val perimeter: Double
        get() = 2 * (width + height)
}

fun main() {
    val rectangle = Rectangle(5.0, 3.0)
    println(rectangle.description)
    println("Area: ${rectangle.area}")
    println("Perimeter: ${rectangle.perimeter}")
}

Output:

A shape with area 15.0
Area: 15.0
Perimeter: 16.0

🔹 Multiple Interface Implementation

A class can implement multiple interfaces:

interface Flyable {
    fun fly() {
        println("Flying in the sky")
    }
}

interface Swimmable {
    fun swim() {
        println("Swimming in water")
    }
}

class Duck : Flyable, Swimmable {
    fun quack() {
        println("Quack quack!")
    }
}

fun main() {
    val duck = Duck()
    duck.fly()
    duck.swim()
    duck.quack()
}

Output:

Flying in the sky
Swimming in water
Quack quack!

🔹 Resolving Interface Conflicts

When multiple interfaces have the same method, you must override it:

interface A {
    fun foo() {
        println("A")
    }
}

interface B {
    fun foo() {
        println("B")
    }
}

class C : A, B {
    override fun foo() {
        super<A>.foo()
        super<B>.foo()
        println("C")
    }
}

fun main() {
    val c = C()
    c.foo()
}

Output:

A
B
C

🧠 Test Your Knowledge

What keyword is used to declare an interface in Kotlin?