KOTLIN Tutorial



INTERFACES IN KOTLIN


Interfaces in Kotlin

In Kotlin, an interface defines a contract for classes that implement it. Interfaces can contain methods and properties, but unlike abstract classes, they can’t hold state. However, they allow a class to implement multiple interfaces, enabling more flexible designs.

Let’s explore how to work with interfaces in Kotlin!


What is an Interface?

An interface is a collection of abstract methods and properties. Classes can implement interfaces to provide specific behavior for these methods. Kotlin interfaces can have both abstract and concrete methods (methods with implementations).

In Kotlin, an interface is defined using the interface keyword.

Basic Interface Example

interface Animal {
    fun speak()
}

class Dog : Animal {
    override fun speak() {
        println("Woof! Woof!")
    }
}

fun main() {
    val dog = Dog()
    dog.speak() // Calling the method defined in the interface
}
    

Explanation:

  • The Animal interface defines an abstract speak() method.
  • The Dog class implements the Animal interface and provides its own implementation of speak().

Interfaces with Default Method Implementations

In Kotlin, interfaces can contain method implementations. You can define default behavior for methods in an interface, which can be overridden in the implementing class.

Interface with Default Methods

interface Animal {
    fun speak()
    fun sleep() {
        println("Zzz... I'm sleeping!")
    }
}

class Dog : Animal {
    override fun speak() {
        println("Woof! Woof!")
    }
}

fun main() {
    val dog = Dog()
    dog.speak() // Dog's own implementation
    dog.sleep() // Default method from Animal interface
}
    

Explanation:

  • The Animal interface has a sleep() method with a default implementation.
  • The Dog class only needs to override the speak() method while using the default sleep() method.

Multiple Interfaces in Kotlin

One of Kotlin's powerful features is the ability for a class to implement multiple interfaces. This is particularly useful in cases where a class needs to adhere to multiple behaviors or contracts.

Multiple Interface Implementation

interface Animal {
    fun speak()
}

interface Swimmer {
    fun swim()
}

class Duck : Animal, Swimmer {
    override fun speak() {
        println("Quack! Quack!")
    }

    override fun swim() {
        println("The duck is swimming!")
    }
}

fun main() {
    val duck = Duck()
    duck.speak() // Implements Animal's speak
    duck.swim()  // Implements Swimmer's swim
}
    

Explanation:

  • The Duck class implements both the Animal and Swimmer interfaces.
  • It provides implementations for both speak() and swim() methods.

Interface with Properties

Interfaces in Kotlin can also define properties. However, the property declarations in the interface are abstract, so classes implementing the interface must provide their own implementations of the properties.

Interface with Properties

interface Animal {
    val name: String
    fun speak()
}

class Dog(override val name: String) : Animal {
    override fun speak() {
        println("$name says Woof!")
    }
}

fun main() {
    val dog = Dog("Buddy")
    println("Animal Name: ${dog.name}") // Accessing property from interface
    dog.speak() // Calling method from interface
}
    

Explanation:

  • The Animal interface defines an abstract property name.
  • The Dog class provides an implementation for the name property and the speak() method.

πŸ§ͺ Try It Yourself

Here are some interactive challenges for you to practice with interfaces:

  • Create an interface Flyable with a method fly(). Implement it in classes like Bird and Airplane.
  • Design an interface Shape with a property area and implement it in classes like Circle and Rectangle.
  • Make a Workable interface with a method work(). Create classes Engineer and Manager that implement the interface and override the method.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review