Q:

Kotlin program | Example of Interface

belongs to collection: Kotlin Class and Object Programs

0

Interface

  • Kotlin interfaces can contain abstracts methods as well as concrete methods(methods with implementations).
  • Kotlin interfaces can have properties but these need to be abstract or to provide accessor implementations.
  • Interfaces cannot store state, which makes them different from abstract classes.
  • Kotlin methods and properties by default abstract, if no implementation provided.
  • Kotlin's class can implement one or more interfaces.

All Answers

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

Program demonstrate the example of Interface in Kotlin

package com.includehelp

// Declare Interface
interface Organization{
    // Abstract Property
    val age:Int

    // Property with accessor implementations
    val name:String
        get() = "Trump"

    // interface method with implementations
    fun printAge(){
        println("Age : $age")
    }

    // abstract method
    fun getSalary(salary:Int)

}

// class implements interface
class Manager:Organization{
    // override interface abstract property
    override val age=32

    // Override interface abstracts method
    override fun getSalary(salary: Int) {
        println("Your Salary : $salary")
    }
}

// Main function, Entry Point of Program
fun main(){
    // Create instance of class
    val organization=Manager()

    // Call function
    organization.printAge()

    // Call function
    organization.getSalary(10000)

    // Access properties
    println("Name : ${organization.name}")
}

Output:

Age : 32
Your Salary : 10000
Name : Trump

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

total answers (1)

Kotlin program | Example of Inheritance within Int... >>
<< Kotlin program | Example of Init in a Class...