In Java, I can define an interface nested inside MainClass
, and I can still reference that interface via any SubClass
, like this:
public class MainClass {
interface MyInterface {
public void printName();
}
}
public class SubClass extends MainClass {
}
// Notice here that I use `SubClass.MyInterface` so the client doesn't know MainClass exists.
public void handleInterface(SubClass.MyInterface implementation) {
}
However, if I try to do the same in Kotlin, it does not work:
open class MainClass {
interface MyInterface {
fun printName()
}
}
class SubClass : MainClass()
// This will not compile unless I do `MainClass.MyInterface`. See the Java notes about why I might want that.
fun handleInterface(implementation: SubClass.MyInterface) {
}
I have already had the debate that we don't need SubClass.MyInterface
and can just reference it from the base class where it was defined, but I really am curious why Kotlin doesn't support this but Java seems to allow it.