ADVERTISEMENT
ADVERTISEMENT

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.