June 27, 2023

Kotlin -- interfaces

Like Java, Kotlin supports interfaces.

My attitude has always been, "who the heck needs interfaces?". I program in other OO languages (ruby, python) and don't find interfaces. One explanation is that interfaces can provide something like multiple inheritance (something else I don't find myself needing). Ruby has "mixins" to allow something resembling multiple inheritance. Python allows multiple inheritance (another strike against it) so there is no need for interfaces.

But let's forget about this multiple inheritance business and take a look at what a Java/Kotlin interface is.

An interface looks a lot like a class. It has abstract methods (methods with signatures but no implementation) along with constants (final variables). The interface does not specify which class provides the implementation. A class that does provide the implementation will look like this:
public class Circle implements Shape { ... }
Here "Shape" is the interface and "Circle" is the class that implements it. This means that Circle should provide the expected methods.

Abstract classes

Java also provides these, typically as a base class that will be extended by other not abstract classes. An abstract class may provide implementations for some (or all) methods.
Have any comments? Questions? Drop me a line!

Adventures in Computing / [email protected]