Kotlin program | Example of Interface

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

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.

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



Comments and Discussions!

Load comments ↻






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