Open In App

Kotlin Interfaces

Last Updated : 08 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, an interface is a collection of abstract methods and properties that define a common contract for classes that implement the interface. An interface is similar to an abstract class, but it can be implemented by multiple classes, and it cannot have state.

Interfaces are custom types provided by Kotlin that cannot be instantiated directly. Instead, these define a form of behavior that the implementing types have to follow. With the interface, you can define a set of properties and methods, that the concrete types must follow and implement.

What Is an Interface?

An interface is a custom type that cannot be instantiated by itself. It can contain:

  1. Abstract methods (method signature without implementation).
  2. Method declarations with default implementations.
  3. Property declarations, possibly with custom getters (but no backing fields).
  4. Default values for method parameters.

A class (or object) can implement one or more interfaces. When it does, it must provide concrete implementations for all abstract members, or explicitly inherit default implementations. Because Kotlin supports single inheritance for classes but allows multiple interface conformance, interfaces are a flexible tool for sharing behavior across unrelated classes.

Creating Interfaces

The interface definition in Kotlin begins with the interface keyword followed by the name of the interface, followed by the curly braces within which the members of the interface reside. The difference is that the members will have no definition of their own. These definitions will be provided by the conforming types. 

Example:

Kotlin
// Define a simple interface named Vehicle
interface Vehicle {
    // Abstract method: no implementation provided here
    fun start()

    // Abstract method: must be implemented
    fun stop()
}


Implementing Interfaces -

An interface can be implemented by a class or an object. When implementing an interface, the conforming type must provide the definition for all of its members. To implement an interface, the name of the custom type is followed by a colon and the name of the interface which is to be implemented.

class Car: Vehicle

Example - 

Java
interface Vehicle {
    fun start()
    fun stop()
}

// Implementation of the Vehicle interface
class Car : Vehicle {
    // Override all abstract members:
    override fun start() {
        println("Car started")
    }

    override fun stop() {
        println("Car stopped")
    }
}

fun main() {
    val myCar = Car()
    myCar.start()
    myCar.stop()
}

Output:

Car started
Car stopped

Explanation: In this program, the interface Vehicle declares two methods start() and stop(), which need to be overridden. The class Car implements the interface using the class-literal syntax and overrides the two methods using the override keyword. Finally, the main function creates an object of class Car and calls the two methods.

Default Implementations and Default Parameters -

Methods in an interface can have default values for its parameters. If the value for a parameter is not provided at the time of function call, then the default value is used. Also, the methods can have default implementations. These are used in the case where the method is not overridden.

Example - 

Java
interface FirstInterface {
    // 'b' has a default value of 5
    fun add(a: Int, b: Int = 5)
    fun print() {
        println("This is a default method defined in the interface")
    }
}

class InterfaceDemo : FirstInterface {
    override fun add(a: Int, b: Int): Int {
        return a + b
    }
    override fun print() {
        super.print()
        println("It has been overridden")
    }
}

fun main() {
    val demo = InterfaceDemo()
    println("Sum is ${demo.add(5)}")    // Only 'a' provided; 'b' defaults to 5
    println("Sum is ${demo.add(5, 10)}") // Both 'a' and 'b' provided
    // Calls the overridden print() in InterfaceDemo
    demo.print()
}

Output:

Sum is 10
Sum is 15
It has been overridden

Explanation: In the above program, the FirstInterface defines a single method add(). The add() method has two parameters, one of which is provided a default value of 5. So, when class InterfaceDemo implements the interface, it overrides the method. Also, in main function first only one argument is specified when calling add method, since the second one is given a default value and in finally both a and b values are provided.

Properties in interface -

Just like methods, interfaces can also contain properties. However, since the interface doesn't have a state that is, they can't be instantiated, so there are no backing fields to hold their values. Hence, the fields in the interface are either left abstract or are provided an implementation.

Example - 

Java
interface InterfaceProperties {
    // Abstract property: must be overridden
    val a: Int

    // Property with a getter: default implementation (no backing field)
    val b: String
        get() = "Property from Interface: $a"
}

class PropertiesDemo : InterfaceProperties {
    // Provide concrete value for 'a'
    override val a: Int = 5000

    // You may override 'b' if desired, but not required
    override val b: String
        get() = "Overridden: $a"
}

fun main() {
    val demo = PropertiesDemo()
    println(demo.a)
    println(demo.b)
}

Output:

5000
Overridden: 5000

Explanation: In the above program, InterfaceProperties defines two properties a which is an integer, and b of type String which is provided a getter. The class PropertiesDemo implements InterfaceProperties and overrides the two properties, providing them value. The function main creates an object of the class and accesses the properties using dot-syntax.

Inheritance in Interfaces -

Interfaces in Kotlin can also inherit other interfaces. When an interface extends another interface, it can add its own properties and methods, and the implementing type has to provide a definition for all the properties and methods in both the interfaces. An interface can inherit more than one interface.

Example - 

Java
// Base interface with two abstract properties
interface Dimensions {
    val length: Double
    val breadth: Double
}

// Interface extending Dimensions and adding methods
interface CalculatedParameters : Dimensions {
    fun area(): Double
    fun perimeter(): Double
}

class Rectangle(override val length: Double, override val breadth: Double) : CalculatedParameters {
    // Implement area() and perimeter() from CalculatedParameters:
    override fun area(): Double {
        return length * breadth
    }

    override fun perimeter(): Double {
        return 2 * (length + breadth)
    }
}

fun main() {
    val rect = Rectangle(length = 10.0, breadth = 5.0)
    println("Area is ${rect.area()}")
    println("Perimeter is ${rect.perimeter()}")
}

Output:

Area is 50.0
Perimeter is 30.0

Explanation: In the program, the interface Dimensions define two properties length and breadth. The interface CalculatedParameters inherits Dimensions and adds two methods area() and perimeter(). The class XYZ implements CalculatedParameters and overrides both the properties and methods, which then invoked in main function.

Multiple Interface Implementation -

Since classes in Kotlin follow the concept of single inheritance, that is, each class can inherit only class, however, in case of interfaces a class supports multiple inheritance, also known as multiple conformance in Kotlin. A class can implement more than one interface, provided that it provides a definition for all the members of the interface.

Example:

Java
interface InterfaceProperties {
    val info: String
}

interface InterfaceMethods {
    fun showInfo()
}

class MultipleInterface : InterfaceProperties, InterfaceMethods {
    // Implement the property from InterfaceProperties
    override val info: String = "Multiple Interfaces implemented"

    // Implement the method from InterfaceMethods
    override fun showInfo() {
        println(info)
    }
}

fun main() {
    val obj = MultipleInterface()
    obj.showInfo() 
}

Output:

Multiple Interfaces implemented

Explanation: In the program, two interfaces InterfaceProperties and InterfaceMethods are defined. These interfaces are implemented by the class MultipleInterface and then the methods are invoked in main function.

If two interfaces declare a method with the same signature and both provide default implementations, you must explicitly override and delegate to whichever implementation you want (or combine them)

Example:

Kotlin
interface A {
    fun greet() { println("Hello from A") }
}
interface B {
    fun greet() { println("Hello from B") }
}
class C : A, B {
    override fun greet() {
        // Combine or choose one:
        super<A>.greet()
        super<B>.greet()
    }
}


Advantages of using interfaces in Kotlin:

  1. Abstraction: Interfaces provide a way to define a common contract between different classes without specifying the implementation details. This enables you to create abstractions that improve the modularity and maintainability of your code.
  2. Polymorphism: Interfaces allow you to create objects of different types that have the same interface, which enables polymorphic behavior.
  3. Code Reusability: Interfaces provide a way to reuse code by allowing multiple classes to implement the same interface and share the same abstract methods and properties.

Disadvantages of using interfaces in Kotlin:

  1. Limited Implementation: Interfaces can only contain abstract methods and properties, so they provide limited implementation details.
  2. Complexity: Interfaces can make your code more complex, especially if you have many classes that implement multiple interfaces.

Reference:

A popular book for learning Kotlin is "Kotlin in Action" by Dmitry Jemerov and Svetlana Isakova. This book provides a comprehensive introduction to the Kotlin programming language, including its syntax, libraries, and best practices, with many hands-on examples and exercises.


Next Article
Article Tags :

Similar Reads