Javatpoint Logo
Javatpoint Logo

Kotlin Interface

An interface is a blueprint of class.Kotlin interface is similar to Java 8. It contains abstract method declarations as well as implementation of method.

Defining Interface

An interface is defined using the keyword interface. For example:

The methods which are only declared without their method body are abstract by default.

Why use Kotlin interface?

Following are the reasons to use interface:

  • Using interface supports functionality of multiple inheritance.
  • It can be used achieve to loose coupling.
  • It is used to achieve abstraction.

Subclass extends only one super class but implements multiple interfaces. Extension of parent class or interface implementation are done using (:) operator in their subclass.

Implementing Interfaces

In this example, we are implementing the interface MyInterface in InterfaceImp class. InterfaceImp class provides the implementation of property id and abstract method absMethod() declared in MyInterface interface.

Output:

Calling overriding id value = 101
MyInterface doing some work
Implementing abstract method..

Implementing multiple interface

We can implement multiple abstract methods of different interfaces in same class. All the abstract methods must be implemented in subclass. The other non-abstract methods of interface can be called from derived class.

For example, creating two interface MyInterface1 and MyInterface2 with abstract methods doSomthing() and absMethod() respectively. These abstract methods are overridden in derive class MyClass.

Output:

overriding doSomthing() of MyInterface1
overriding absMethod() of MyInterface2

Resolving different Interfaces having same method overriding conflicts

Let's see an example in which interface MyInterface1 and interface MyInterface2 both contains same non-abstract method. A class MyClass provides the implementation of these interfaces. Calling the method of interface using object of MyClass generates an error.

Output:

Kotlin: Class 'MyClass' must override public open fun doSomthing(): Unit defined in MyInterface1 because it 
inherits multiple interface methods of it

To solve the above problem we need to specify particular method of interface which we are calling. Let's see an example below.

In below example, two interfaces MyInterface1 and MyInterface2 contain two abstract methodsadsMethod() and absMethod(name: String) and non-abstract method doSomthing() in both respectively. A class MyClass implements both interface and override abstract method absMethod() and absMethod(name: String) . To override the non-abstract method doSomthing() we need to specify interface name with method using super keyword as super<interface_name>.methodName().

Output:

MyInterface 2 doing some work
Implements absMethod() of MyInterface1
Implements absMethod(name) of MyInterface2 name is  Ashu

Output:

MyInterface 1 doing some work
MyInterface 2 doing some work
MyInterface 2 absMethod
MyInterface 1 doing some work
MyInterface1 absMethod implementation

Next TopicKotlin Data Class




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA