Implement Interface in Kotlin - GeeksforGeeks

13 Jan, 2022
Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store a state. They can have properties, but these need to be abstract or provide accessor implementations.
A Kotlin interface contains declarations of abstract methods, and default method implementations although they cannot store state.
interface MyInterface {
    fun bar()
}
This interface can now be implemented by a class as follows:
class Child : MyInterface {
    override fun bar() {
        print("bar() was called")
    }
}
An interface in Kotlin can have default implementations for functions:
interface MyInterface {
    fun withImplementation() {
        print("withImplementation() was called")
    }
}
Classes implementing such interfaces will be able to use those functions without reimplementing
class MyClass: MyInterface {
    // No need to reimplement here
}
    val instance = MyClass()
    instance.withImplementation()
Default implementations also work for property getters and setters:
interface MyInterface2 {
 val helloWorld
 get() = "Hello World!"
}
Interface accessors implementations can’t use backing fields
interface MyInterface3 {
    // this property won't compile!
    var helloWorld: Int
    get() = field
    set(value) { field = value }
}
When multiple interfaces implement the same function, or all of them define with one or more implementing, the derived class needs to manually resolve proper call
Kotlin
interface A {   fun notImplemented()   fun implementedOnlyInA() { print("only A") }   fun implementedInBoth() { print("both, A") }   fun implementedInOne() { print("implemented in A") }}  interface B {   fun implementedInBoth() { print("both, B") }   // only defined   fun implementedInOne() }  class MyClass: A, B {   override fun notImplemented() { print("Normal implementation") }   // implementedOnlyInA() can by normally used in instances   // class needs to define how to use interface functions   override fun implementedInBoth() {   super<B>.implementedInBoth()   super<A>.implementedInBoth() }     // even if there's only one implementation,// there multiple definitionsoverride fun implementedInOne() {   super<A>.implementedInOne()   print("implementedInOne class implementation") }}
You can declare properties in interfaces. Since an interface cannot have stated you can only declare a property as abstract or by providing default implementation for the accessors.
Kotlin
interface MyInterface {  // abstract  val property: Int   val propertyWithImplementation: String      get() = "foo"  fun foo() {    print(property)  }}  class Child : MyInterface {  override val property: Int = 29}
When implementing more than one interface that has methods of the same name that include default implementations, it is ambiguous to the compiler which implementation should be used. In the case of a conflict, the developer must override the conflicting method and provide a custom implementation. That implementation may choose to delegate to the default implementations or not.
Kotlin
interface FirstTrait { fun foo() { print("first") } fun bar()}  interface SecondTrait { fun foo() { print("second") } fun bar() { print("bar") }}  class ClassWithConflict : FirstTrait, SecondTrait { override fun foo() {     // delegate to the default     // implementation of FirstTrait     super<FirstTrait>.foo()      // delegate to the default      // implementation of SecondTrait     super<SecondTrait>.foo()  } // function bar() only has a default implementation // in one interface and therefore is ok.}
interface MyInterface {
 fun funcOne() {
     // optional body
     print("Function with default implementation")
 }
}
Note:
If the method in the interface has its own default implementation, we can use the super keyword to access it.
super.funcOne()
Kotlin OOPs
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Retrofit with Kotlin Coroutine in Android
How to Get Current Location in Android?
ImageView in Android with Example
How to Build a Weather App in Android?
Android SQLite Database in Kotlin
ScrollView in Android
Suspend Function In Kotlin Coroutines
Kotlin Coroutines on Android
How to Use View Binding in RecyclerView Adapter Class in Android?
Notifications in Android with Example