Interfaces

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 state. They can have properties but these need to be abstract or to provide accessor implementations.

An interface is defined using the keyword interface

  1. interface MyInterface {
  2. fun bar()
  3. fun foo() {
  4. // optional body
  5. }
  6. }

Implementing Interfaces

A class or object can implement one or more interfaces

  1. class Child : MyInterface {
  2. override fun bar() {
  3. // body
  4. }
  5. }

Properties in Interfaces

You can declare properties in interfaces. A property declared in an interface can either be abstract, or it can provide implementations for accessors. Properties declared in interfaces can’t have backing fields, and therefore accessors declared in interfaces can’t reference them.

  1. interface MyInterface {
  2. val prop: Int // abstract
  3. val propertyWithImplementation: String
  4. get() = "foo"
  5. fun foo() {
  6. print(prop)
  7. }
  8. }
  9. class Child : MyInterface {
  10. override val prop: Int = 29
  11. }

Interfaces Inheritance

An interface can derive from other interfaces and thus both provide implementations for their members and declare new functions and properties. Quite naturally, classes implementing such an interface are only required to define the missing implementations:

  1. interface Named {
  2. val name: String
  3. }
  4. interface Person : Named {
  5. val firstName: String
  6. val lastName: String
  7. override val name: String get() = "$firstName $lastName"
  8. }
  9. data class Employee(
  10. // implementing 'name' is not required
  11. override val firstName: String,
  12. override val lastName: String,
  13. val position: Position
  14. ) : Person

Resolving overriding conflicts

When we declare many types in our supertype list, it may appear that we inherit more than one implementation of the same method. For example

  1. interface A {
  2. fun foo() { print("A") }
  3. fun bar()
  4. }
  5. interface B {
  6. fun foo() { print("B") }
  7. fun bar() { print("bar") }
  8. }
  9. class C : A {
  10. override fun bar() { print("bar") }
  11. }
  12. class D : A, B {
  13. override fun foo() {
  14. super<A>.foo()
  15. super<B>.foo()
  16. }
  17. override fun bar() {
  18. super<B>.bar()
  19. }
  20. }

Interfaces A and B both declare functions foo() and bar(). Both of them implement foo(), but only B implements bar() (bar() is not marked abstract in A, because this is the default for interfaces, if the function has no body). Now, if we derive a concrete class C from A, we, obviously, have to override bar() and provide an implementation.

However, if we derive D from A and B, we need to implement all the methods which we have inherited from multiple interfaces, and to specify how exactly D should implement them. This rule applies both to methods for which we’ve inherited a single implementation (bar()) and multiple implementations (foo()).