🔌 Kotlin Interfaces – Complete Guide with Examples (2025)
Understand how interfaces work in Kotlin, how they differ from abstract classes, and how to implement multiple interfaces with real code examples.
🚀 What is an Interface in Kotlin?
An interface in Kotlin defines a contract that classes can implement. It can contain:
- Abstract methods (without implementation)
- Concrete methods (with default implementation) — from Kotlin 1.1+
- Properties (abstract or with getters)
📌 Use interfaces when you want to define capabilities that can be shared across unrelated classes.
🧱 Kotlin Interface Syntax
interface Drivable {
fun drive()
fun stop() {
println("Vehicle stopped.")
}
}
drive()is abstract.stop()has a default implementation.
🚗 Implementing an Interface
class Car : Drivable {
override fun drive() {
println("Driving the car...")
}
}
fun main() {
val myCar = Car()
myCar.drive() // Output: Driving the car...
myCar.stop() // Output: Vehicle stopped.
}
✅ The class must override all abstract methods in the interface.
🔁 Multiple Interfaces
Kotlin supports multiple interface inheritance, enabling classes to implement multiple behaviors:
interface Drivable {
fun drive()
}
interface Flyable {
fun fly()
}
class FlyingCar : Drivable, Flyable {
override fun drive() = println("FlyingCar is driving.")
override fun fly() = println("FlyingCar is flying.")
}
🧠 This allows composition of behaviors, similar to mixins.
🧮 Interfaces with Properties
interface Shape {
val name: String
fun draw()
}
class Circle : Shape {
override val name = "Circle"
override fun draw() = println("Drawing a circle.")
}
- Properties can be declared in interfaces.
- Must be overridden or have a default getter.
🤺 Interface Inheritance
Interfaces can extend other interfaces:
interface Machine {
fun operate()
}
interface SmartMachine : Machine {
fun selfCheck()
}
A class implementing SmartMachine must also implement operate().
🧭 Interface vs Abstract Class – Quick Comparison
| Feature | Interface | Abstract Class |
|---|---|---|
| Multiple inheritance | ✅ Yes | ❌ No |
| Method implementation | ✅ Yes (default methods) | ✅ Yes |
| Properties | ✅ Abstract or with getters | ✅ Full property support |
| Constructors | ❌ Not allowed | ✅ Allowed |
| Use case | Behavior contracts | Base classes with logic |
📋 Summary
| Concept | Description |
|---|---|
interface | Defines a contract of abstract + optional methods |
default method | Function with implementation (from Kotlin 1.1+) |
multiple interfaces | Kotlin supports implementing multiple interfaces |
interface inheritance | Interfaces can extend other interfaces |
properties | Can be abstract or have custom getter |
❓ FAQ – Kotlin Interfaces
🔹 Q1: Can an interface have a constructor?
A: No. Interfaces in Kotlin cannot have constructors or state (fields).
🔹 Q2: Can you inherit from multiple interfaces in Kotlin?
A: Yes. Kotlin supports multiple interface inheritance.
🔹 Q3: Can you override default methods in interfaces?
A: Yes. Classes can override default methods provided in interfaces.
interface Greetable {
fun greet() = println("Hello!")
}
class User : Greetable {
override fun greet() = println("Hi there!")
}
🔹 Q4: Can interfaces contain properties?
A: Yes, but:
- They must be abstract, or
- Have only getter implementations.
🔹 Q5: Is interface the same as abstract class?
A: No. Use interface for defining capabilities and abstract class for shared code base and inheritance.
Share Now :
