Kotlin Interfaces

Kotlin Interfaces : Kotlin Interfaces are similar to Kotlin Classes, and can contain uninitialized variables, abstract and non-abstract methods.

If a class is implementing an interface, the class is making a promise that the abstract methods in the interface shall be implemented. Compiler makes sure that this promise is not broken and if it does, compiler throws an error and stops.

Kotlin Interface Example

Following is a basic Kotlin Interface Example :

  • interface keyword is used to define an interface.
  • Kotlin Interface cannot have initialized variables.
  • Kotlin Interface can have non-abstract and abstract methods.

Implement an Interface

Kotlin Classes can implement an interface and these classes should override the abstract methods and variables of Kotlin Interface. Following is an example where Aeroplane class implements Vehicle interface.

Implement multiple interfaces

A Kotlin Class can implement multiple Kotlin Interfaces. All the abstract methods and variables of the interfaces being implemented must be overridden in the class.

Conclusion :

In this Kotlin Tutorial, we have learnt about Kotlin Interfaces and how to implement them. In our next tutorial, we shall learn Kotlin Abstract Classes.