Thuta Learning
IntermediateProgrammingbeginner

Interfaces

Relax. We'll talk through this in plain words — no textbook voice.

An interface is a contract that defines what a class must be able to do. A class can implement more than one interface, which makes it useful for organizing behavior cleanly.

kotlin
interface Drivable {
    fun drive()
}

class Car : Drivable {
    override fun drive() {
        println("Driving a car")
    }
}

fun main() {
    val car = Car()
    car.drive()
}
You should see
Driving a car

Summary

Interfaces let you write classes against a shared, well-organized contract.

Easy traps

  • If you implement an interface but don't write its required function, you'll get a compile error.

Practical example — payable

Practical example — payable

kotlin
interface Payable {
    fun pay(amount: Int)
}

class MobileWallet : Payable {
    override fun pay(amount: Int) {
        println("Paid $amount MMK with mobile wallet")
    }
}

fun main() {
    val wallet = MobileWallet()
    wallet.pay(10000)
}

You'll know it worked when: Paid 10000 MMK with mobile wallet

Interfaces | Thuta Learning