Loading, please wait...

A to Z Full Forms and Acronyms

Explain Interface in Kotlin | Kotlin Tutorial

Jan 27, 2022 #KotlinLanguage #Programming, 1474 Views
In this article, you will learn about the following topics: What is the interface in kotlin? Characteristics of the interface in kotlin. Implementation of the interface. Implementation of multiple interfaces.

Explain Interface in Kotlin | Kotlin Tutorial

In this article, you will learn about the following topics:

  • What is the interface in kotlin?
  • Characteristics of the interface in kotlin.
  • Implementation of the interface.
  • Implementation of multiple interfaces.

What is the interface in kotlin?

The blueprint of the class is known as the interface. It is very similar to Java 8. It contains an abstract method declaration along with the implementation of the method. 

Syntax of interface:

An interface is declared by using the keyword “interface”

Example of interface:

interface interface_name{

val id:int // abstract property

    fun abc() // abstract method

    fun xyz() {

       // body of the function

    }

}

The methods declared without body are by default abstract.

Characteristics of the interface in the kotlin language:

There are reasons to use the concept of the interface in kotlin:

  • If we are using the interface in kotlin, it will support the functionality of multiple inheritance. 
  • It helps in achieving the property of abstraction. 
  • It is used to achieve loose coupling.

Subclasses are allowed to extend only one superclass but it is allowed to implement multiple interfaces. The implementation of the interface can be using (:) colon operator. 

Implementation of the interface

interface Inter_face  {  
var i: Int            // abstract property  
    fun abc():String    // abstract method  
    fun xyz() {  
println("Interface declaration")  
    }  
}  
class Interface_class : MyInterface {  
    override var i: Int = 2
    override fun abc(): String{  
return "Implementing abstract method.."  
    }  
}  
fun main(args: Array<String>) {  
val obj = InterfaceImp()  
println("Calling overriding id value = ${obj.i}")  
obj.xyz()  
println(obj.abc())  
}  

In this example, we create an interface named inter_face that has one abstract variable I, one abstract method abc(), and one normal method xyz(). The interface is implemented in the Interface_class and the variable and abc() method is overridden in the class. 

The output of the following code is as follow:

Calling overriding id value = 2
Interface declaration
Implementing abstract method. .

Implementation of multiple interfaces

interface Interface1 {  
    fun abc()  
}  
interface Interface2 {  
    fun xyz()  
}  
class interface_imp : Interface1, Interface2 {  
    override fun abc() {  
println("overriding abc() of Interface1")  
    }  
  
    override fun xyz() {  
println("overriding xyz() of Interface2")  
    }  
}  
fun main(args: Array<String>) {  
val myClass = MyClass()  
myClass.abc()  
myClass.xyz()  
} 


In this example, we have two interfaces named Interface1, Interface2 and both have abstract methods i.e abc() and xyz(). 

The output of the following code is:

overriding abc() of Interface1
overriding xyz() of Interface2

A to Z Full Forms and Acronyms

Related Article