Kotlin Interface Examples
Interface define contract for classes. An interface is similar to a class just that, it's a collection of abstract methods and variables. Essentially Interfaces are one level more abstract than abstract classes. Let's say we have an interface called Driveable. interface Driveable { fun drive() // declaring 'abstract' is implicit and unnecessary } we can then have different vehicles (e.g Car, Truck, Lorry, Motorcycle, Ship, Plane etc) implement the interface and override the drive method. now let's see how we implement it interface Driveable { fun drive() fun refuel() } class Car(var color : String) : Driveable{ override fun drive() { println("Driving my $color car...") } override fun refuel() { println("Re-Fueling my car ...") } } class Truck (var color: String) : Driveable{ override fun drive() { println("Driving my $color Truck...") } override fun refuel() { ...