← Back to Kotlin Course | Chapter 7: Inheritance & Interfaces | Lesson 4 of 7

Interface Basics

An interface is a promise a class makes to do certain things, like agreeing to a checklist of abilities without saying exactly how.
Syntax
kotlin
interface InterfaceName {
    fun method(): ReturnType
    fun withDefault(): ReturnType = value
}

class ClassName : InterfaceName {
    override fun method(): ReturnType = value
}

Declaring an Interface

An interface defines a set of functions and properties that any implementing class agrees to provide, without specifying how they must be implemented.

Example: Declaring an Interface

markup
// Define an interface `Greetable` describing required behavior
interface Greetable {
    // Define the function `greet` with no parameters and returning a String
    fun greet(): String
}

// Define a class `Person`
class Person(val name: String) : Greetable {
    override fun greet(): String = "Hello, I am $name"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `person`, set to `Person("Ana")`
    val person = Person("Ana")
    // Print `person.greet()` to the console, with a trailing newline
    println(person.greet())
}

Default Implementations in Interfaces

An interface function can include a body directly, providing a default implementation that classes may use as-is or override with their own version.

Example: Default Implementations in Interfaces

markup
// Define an interface `Greetable` describing required behavior
interface Greetable {
    // Define the function `greet` with no parameters and returning a String
    fun greet(): String = "Hello there!"
}

// Define a class `Robot`
class Robot : Greetable

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `robot`, set to `Robot()`
    val robot = Robot()
    // Print `robot.greet()` to the console, with a trailing newline
    println(robot.greet())
}

Implementing an Interface

A class implements an interface by listing it after a colon, then providing override for any of its abstract members that have no default implementation.

Example: Implementing an Interface

markup
// Define an interface `Playable` describing required behavior
interface Playable {
    // Define the function `play` with no parameters and returning a String
    fun play(): String
}

// Define a class `Song`
class Song(val title: String) : Playable {
    override fun play(): String = "Playing $title"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `song`, set to `Song("Kotlin Beats")`
    val song = Song("Kotlin Beats")
    // Print `song.play()` to the console, with a trailing newline
    println(song.play())
}

Properties in Interfaces

Interfaces can declare abstract properties too, which implementing classes must provide a value or backing implementation for.

Example: Properties in Interfaces

markup
// Define an interface `HasArea` describing required behavior
interface HasArea {
    val area: Double
}

// Define a class `Square`
class Square(val side: Double) : HasArea {
    override val area: Double
        get() = side * side
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `square`, set to `Square(4.0)`
    val square = Square(4.0)
    // Print "Area: ${square.area}" to the console, with a trailing newline
    println("Area: ${square.area}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to store constructor-style state in an interface; interfaces cannot have constructors or backing-field state, only abstract or default-implemented members.
  2. Forgetting to implement a required interface method, which the compiler flags as a missing override in the implementing class.
  3. Assuming an interface method without a body is automatically abstract-only; interfaces can also provide default implementations directly.
Chapter Summary
  • interface declares a contract of functions and properties that implementing classes must fulfill.
  • Interface members are abstract by default but can also provide a default implementation directly in the interface.
  • A class implements an interface with class MyClass : MyInterface { ... }, providing override for any required members.
  • A class can implement multiple interfaces, unlike single-inheritance from classes.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.