Kotlin program | Example of Inheritance within Interface

Kotlin | Inheritance within Interface: Here, we are implementing a Kotlin program to demonstrate the example of inheritance within interface.
Submitted by IncludeHelp, on June 08, 2020

Inheritance within Interface

  • An Interface Derive from other interfaces.
  • The derived interface can override super interface members or declare new functions and properties
  • Kotlin class implementing such an interface are only need to define missing implementations

Program demonstrate the example of Inheritance within Interface in Kotlin

package com.includehelp

// Declare Interface
interface AA{
    // Abstract property
    val count:Int

    // Abstract methods
    fun sayHello()

    // Method with implementation
    fun sayGudMorning(){
        println("Gud Morning, IncludeHelp !!")
    }
}

// Derived interface from another super interface
interface  BB:AA{
    // Abstract methods
    fun sayBye()

    // Override Interface abstract methods, 
    // declare into super interface
    override fun sayHello() {
        println("Hello, IncludeHelp !!")
    }
}

// Declare class implementing interface
class IncludeHelp:BB{
    // Override abstract property
    override val count: Int
        get() = 100

    // Override Interface abstract methods
    override fun sayBye() {
        println("Gud Bye, IncludeHelp !!")
    }
}

// Main function, entry point of program
fun main(){
    // Create instance of class
    val includeHelp = IncludeHelp()

    // Call method
    includeHelp.sayGudMorning()

    // Call method
    includeHelp.sayHello()

    // Call method
    includeHelp.sayBye()

    println("Abstract Property : ${includeHelp.count}")
}

Output:

Gud Morning, IncludeHelp !!
Hello, IncludeHelp !!
Gud Bye, IncludeHelp !!
Abstract Property : 100


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.