Kotlin Interfaces
Kotlin interfaces are similar to Java 8. The interface keyword is used to define interfaces, allowing methods to have default implementations:
Implementing Interfaces
A class or object can implement one or more interfaces.
Example
The output result is:
foo bar
Properties in Interfaces
Properties in interfaces can only be abstract; initialization values are not allowed. Interfaces do not store property values. When implementing an interface, the properties must be overridden:
Example
The output result is:
foo bar example
Function Overriding
When implementing multiple interfaces, you may encounter the problem of inheriting multiple implementations of the same method. For example:
Example
The output result is:
ABbar
In the example, interfaces A and B both define methods foo() and bar(). Both implement foo(), and B implements bar(). Because C is a concrete class that implements A, it must override bar() and implement this abstract method.
However, if we derive D from A and B, we need to implement all methods inherited from multiple interfaces and specify how D should implement them. This rule applies both to methods that inherit a single implementation (bar()) and to methods that inherit multiple implementations (foo()).
Other Extensions