codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

14 / 68 topics
11Extensions Functions12Classes and Objects13Inheritance in Kotlin14Interfaces in Kotlin15Data Classes16Sealed Classes17Companion Objects18Generics in Kotlin19Type Aliases20Properties and Fields21Lateinit Properties22Delegated Properties
Tutorials/Kotlin/Interfaces in Kotlin
🎯Kotlin

Interfaces in Kotlin

Updated 2026-04-20
4 min read

Interfaces in Kotlin

Interfaces are a fundamental concept in Kotlin, providing a way to define a contract for classes without specifying how they should implement it. This tutorial will cover the basics of interfaces in Kotlin, including their syntax, usage, and best practices.

Introduction to Interfaces

An interface in Kotlin is an abstract type that can contain declarations of methods, properties, and nested types. Unlike classes, interfaces cannot store state (i.e., they cannot have fields). They are primarily used to define a contract for what a class should do, without specifying how it does it.

Declaring an Interface

To declare an interface in Kotlin, use the interface keyword followed by the interface name and its body. Here's a simple example:

interface Animal {
    fun makeSound()
}

In this example, the Animal interface declares a single method makeSound().

Implementing Interfaces

Classes can implement one or more interfaces using the : symbol followed by the interface names. When a class implements an interface, it must provide implementations for all the methods declared in the interface.

Basic Implementation

Here's how you can implement the Animal interface in a class:

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

fun main() {
    val dog = Dog()
    dog.makeSound()  // Output: Woof!
}

In this example, the Dog class implements the Animal interface and provides an implementation for the makeSound() method.

Multiple Interface Implementation

Kotlin allows a class to implement multiple interfaces. Here's an example:

interface Flyable {
    fun fly()
}

class Bird : Animal, Flyable {
    override fun makeSound() {
        println("Chirp!")
    }

    override fun fly() {
        println("Flying high in the sky!")
    }
}

fun main() {
    val bird = Bird()
    bird.makeSound()  // Output: Chirp!
    bird.fly()       // Output: Flying high in the sky!
}

In this example, the Bird class implements both the Animal and Flyable interfaces.

Properties in Interfaces

Interfaces can also declare properties. However, they cannot have backing fields, so you must provide an implementation for the property in any implementing class.

Property Declaration

Here's how you can declare a property in an interface:

interface Vehicle {
    val wheels: Int
}

In this example, the Vehicle interface declares a property wheels.

Property Implementation

To implement the wheels property, you must provide a value for it in any implementing class:

class Car : Vehicle {
    override val wheels: Int = 4
}

fun main() {
    val car = Car()
    println("Car has ${car.wheels} wheels.")  // Output: Car has 4 wheels.
}

In this example, the Car class implements the Vehicle interface and provides a value for the wheels property.

Default Implementations

Kotlin interfaces can provide default implementations for methods. This allows you to add new functionality to an interface without breaking existing implementations.

Default Method Implementation

Here's how you can provide a default implementation for a method in an interface:

interface Greeting {
    fun greet(): String {
        return "Hello, Kotlin!"
    }
}

In this example, the Greeting interface provides a default implementation for the greet() method.

Overriding Default Implementations

If a class implements an interface with a default method and you want to provide a custom implementation, you can override it:

class CustomGreeting : Greeting {
    override fun greet(): String {
        return "Hello, World!"
    }
}

fun main() {
    val greeting = CustomGreeting()
    println(greeting.greet())  // Output: Hello, World!
}

In this example, the CustomGreeting class overrides the default implementation of the greet() method.

Interface as a Type

Interfaces can be used as types in Kotlin. This means you can declare variables of an interface type and assign instances of classes that implement the interface to them.

Using Interfaces as Types

Here's an example:

fun printSound(animal: Animal) {
    animal.makeSound()
}

fun main() {
    val dog = Dog()
    val bird = Bird()

    printSound(dog)   // Output: Woof!
    printSound(bird)  // Output: Chirp!
}

In this example, the printSound function takes an Animal as a parameter and calls its makeSound() method. This demonstrates polymorphism in Kotlin.

Best Practices

  1. Single Responsibility Principle: Interfaces should define a single responsibility or capability. Avoid creating interfaces with too many methods that are unrelated to each other.
  2. Default Implementations: Use default implementations judiciously to avoid breaking existing implementations when adding new functionality to an interface.
  3. Interface Segregation Principle: Prefer smaller, specific interfaces over large, general-purpose ones. This makes it easier for classes to implement only the methods they need.
  4. Use Interfaces for Contracts: Interfaces should be used to define contracts that classes must adhere to, rather than as a way to share implementation details.

Conclusion

Interfaces in Kotlin are a powerful tool for defining contracts and promoting code reuse. By understanding how to declare, implement, and use interfaces, you can write more modular and maintainable Kotlin code. Remember to follow best practices to ensure your interfaces remain clear, concise, and effective.


This tutorial provides a comprehensive overview of interfaces in Kotlin, including their syntax, usage, and best practices. By following the examples and guidelines provided, you should be able to effectively use interfaces in your Kotlin projects.


PreviousInheritance in KotlinNext Data Classes

Recommended Gear

Inheritance in KotlinData Classes