Feedback on page "Interfaces" in Language Guide

I am just reading Kotlin Language Guide and I have some feedback as well as questions.
I like the guide. It is precise and concise. But still has some errors(?) and leaves some questions unanswered.
I have my questions after reading page “Interfaces”:

  • I think a snippet of code in topic “Interfaces Inheritance” has an error: class Employee does not declare any implemented interfaces.Does Kotlin support implicit interfaces as in Go? If this is the case then it should be written in that page. Otherwise the error should be fixed because it confuses readers.
  • Is it possible to declare protected members of the interface? This aspect is not covered. I know how it is in Java but other readers may have another background and it may be not so obvious. At the same time Kotlin compiler may behave differently from Java.
  • I assume that all methods in interfaces are open by default unless they are declared final (BTW, is it possible?). It is not covered, so I can only guess or check it with the compiler.
  • I still don’t get how static members of classes (as well as interfaces) are handled in Kotlin. Yes, I have seen “Companion objects” but, first, this is not the same as static member of Java class, and secondary, I am not sure that they are allowed in interfaces.

Theoretically it is possible to have “static virtual” members of classes (not in Java). For example, it is possible in Delphi. Basically static virtual methods are just methods of class itself rather than an instance of the class and they are listed in class VMT as other virtual methods. Static virtual property could be implemented as static virtual get/set class methods that may be backed by a static field. If you have a reference to a class than you can call this class method or access class field which can be virtual.

it does, look at the very last line:

data class Employee(
    // implementing 'name' is not required
    override val firstName: String,
    override val lastName: String,
    val position: Position
) : Person

No, members can only be public and private.

The are exclusively open, final members are not supported.

Kotlin doesn’t have static members. They are instead split between top-level functions (directly in a file instead of nested in a class/object)

fun greet(name: String) {
    println("hello $name!")
}

fun main(){
    greet("Alex")
}

and (companion) objects.

object Greeter {
    fun greet(name: String) {
        println("hello $name!")
    }
}

fun main(){
    Greeter.greet("Alex")
}

The first one creates a JVM static method, objects create a singleton that’s accessible via a static method getInstance().

The concept of static itself is completely encapsulated in Kotlin. You can’t override a top-level function nor can you extend objects. I don’t really see a way to harmonize this with what you’re suggesting.

2 Likes