Interface Basics
In this page:
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Properties in Interfaces
Interfaces can declare abstract properties too, which implementing classes must provide a value or backing implementation for.
Example: Properties in Interfaces
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
- Trying to store constructor-style state in an interface; interfaces cannot have constructors or backing-field state, only abstract or default-implemented members.
- Forgetting to implement a required interface method, which the compiler flags as a missing override in the implementing class.
- Assuming an interface method without a body is automatically abstract-only; interfaces can also provide default implementations directly.
interfacedeclares 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 { ... }, providingoverridefor 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: