Kotlin Interface
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
An Interface is a contract. A contract specifies what the Interface is expected to do.
The how of the interface is left to the implementer of the interface.
Interface in Kotlin can contain only the following. An Interface cannot be instantiated. An implementer must implement the interface.
We can create an interface using the keyword "interface".
interface IEmployee
{
val empId: Int
val company_name: String
fun update()
fun companyName()
{
println(company_name)
}
}
We have created an interface IEmployee with two properties and two functions.
The two properties are empId and company_name.
The two functions are update and companyName. update function is abstract.
Implementing the Interface
Let's see how to implement this interface.
class Employee : IEmployee
{
override val empId = 0
override val company_name = "CosmicLearn"
override fun update()
{
println("updating employee")
}
}
Above we created a class Employee which implemented the interface IEmployee we created earlier.
And override the field empId, company_name and update method.
We can now use this class as follows.
fun main(args: Array<String>)
{
var emp = Employee()
emp.update()
emp.companyName()
}
Output:
updating employee
CosmicLearn