Why is Java prohibiting inheritance of inner interfaces?

前端 未结 3 1957
醉梦人生
醉梦人生 2021-02-18 21:21

I.e. why is the following \"cyclic dependency\" not possible?

public class Something implements Behavior {
    public interface Behavior {
        // ...
    }
}         


        
3条回答
  •  被撕碎了的回忆
    2021-02-18 22:20

    Relevant rules in spec:

    http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.4

    A class C directly depends on a type T if T is mentioned in the extends or implements clause of C either as a superclass or superinterface, or as a qualifier of a superclass or superinterface name.

    http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.1.3

    An interface I directly depends on a type T if T is mentioned in the extends clause of I either as a superinterface or as a qualifier within a superinterface name.

    Therefore if A extends|implements B.C, A depends on both C and B. Spec then forbids circular dependencies.

    The motivation of including B in the dependency is unclear. As you mentioned, if B.C is promoted to top level C2, not much is different as far as the type system is concerned, so why A extends C2 is ok, but not A extends B.C? Granted a nested type B.C does have some prviledged access to B's content, but I can't find anything in spec that makes A extends B.C troublesome.

    The only problem is when C is an inner class. Suppose B=A, A extends A.C should be forbidden, because there's a circular dependency of "enclosing instance". That is probably the real motivation - to forbid outer class from inheriting inner class. The actual rules are more generalized, because they are simpler, and make good sense anyway even for non-inner classes.

提交回复
热议问题