Home »
Kotlin
Interfaces in Kotlin
By IncludeHelp Last updated : December 04, 2024
Prerequisite: abstract class in kotlin
Interfaces
In java we have studied that interface is a collection of only abstract methods. It cannot contain any function with definition. But, Kotlin added more functionality to the interfaces by enabling us to add code in interfaces. Yeah, really we can add code in interfaces.
Creating an Interface
Interface can be created using 'interface' keyword.
interface twoWheeler{
fun getPrice(qt:Int) :Int// abstract method
}
This interface contains an abstract method getPrice(). There is no need of abstract keyword as it is implicitly specified.
Add code to Member Functions of Interface
In kotlin we can add code to the member functions of interface
interface twoWheeler{
fun getDeatils(){ //method with code
println("this is an interface ")
}
}
Interface Implementation
Interfaces are nothing without implementation they must be implemented by some class. They are implemented as like we inherit classes using colon(:) symbol.
class bajaj :twoWheeler{
override fun getPrice(qt: Int) :Int {
return qt*45000
}
}
Here, override keyword is used to implement method of interface and this is compulsory in kotlin.
Interface Properties
Interface can contain properties, see the example
interface twoWheeler{
var price :Int
}
If we initialize the property price like,
var price:Int=45000 // this will produce errror
Properties need to be overridden in the sub class as per the requirement. Use override keyword for that.
class bajaj : twoWheeler{
override var price=45000
}
Kotlin Program to Implement an Interface
In Kotlin, an interface can define methods and properties without providing their implementations. Classes that implement an interface are required to provide implementations for its methods.
interface twoWheeler {
//method with code
fun getDeatils() {
println("This is an interface providing a method getPrice() which returns price as per quantity ")
}
var price: Int // property
fun getPrice(qt: Int): Int // abstract method
}
class bajaj : twoWheeler {
override var price = 45000
override fun getPrice(qt: Int): Int {
return qt * price
}
}
class hero : twoWheeler {
override var price = 50000
override fun getPrice(qt: Int): Int {
return (qt * price + 4500)
}
}
fun main(args: Array<String>) {
var platina = bajaj()
platina.getDeatils()
println("Enter quantity for platina : ")
var qt = readLine()!!.toInt()
print(" Your Price : ")
println(platina.getPrice(qt))
var passion = hero()
println("Enter quantity for passion : ")
qt = readLine()!!.toInt()
print(" Your Price : ")
println(passion.getPrice(qt))
}
Output
This is an interface providing a method getPrice() which returns price as per quantity
Enter quantity for platina :
1
Your Price : 45000
Enter quantity for passion :
1
Your Price : 54500
Through interfaces we can also achieve multiple inheritance
While implementing multiple interfaces a problem may occur that multiple interfaces may contain same function name and complier might confuse while calling them. So to remove ambiguity when calling function with same name, we have to use super keyword then angle braces "<interfaceName>" and then function. As seen in below example.
Kotlin Program to Implement Multiple Interfaces
In Kotlin, a class can implement multiple interfaces. When a class implements multiple interfaces, it must provide implementations for all the methods declared in those interfaces. If there are conflicts, the class must explicitly resolve them.
interface A {
fun display() {
println("this is interface A")
}
}
interface B {
fun display() {
println("this is interface B")
}
}
class C : A, B {
override fun display() {
super<A>.display()
super< B>.display()
}
}
fun main(args: Array<String>) {
var ob = C()
ob.display()
}
Output
this is interface A
this is interface B