Q:

Kotlin program | Example of Inheritance within Interface

belongs to collection: Kotlin Class and Object Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program | Example of Resolving Overriding C... >>
<< Kotlin program | Example of Interface...