Kotlin Interfaces Inheritance Example



An interface can derive from other interfaces and thus both provide implementations for their members and declare new functions and properties. Quite naturally, classes implementing such an interface are only required to define the missing implementations.

Kotlin Interfaces Inheritance Example

package net.javaguides.kotlin.examples

import kotlin.script.dependencies.ScriptContents.Position

interface Named {
    val name: String
}

interface Person: Named {
    val firstName: String
    val lastName: String

    override val name: String get() = "$firstName $lastName"
}

data class Employee(
    // implementing 'name' is not required
    override val firstName: String,
    override val lastName: String,
    val emailId: String
): Person

fun main(args: Array < String > ) {
    val employee = Employee("Ramesh", "Fadatare", "ramesh@gmail.com");
    println(employee.name);
}
Output:
Ramesh Fadatare

Comments