Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Java prohibiting inheritance of inner interfaces?

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

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

Since interfaces don't reference the outer class this should be allowed; however, the compiler is forcing me to define those interfaces outside the class. Is there any logical explanation for this behavior?

like image 465
Philip Kamenarsky Avatar asked Nov 03 '11 10:11

Philip Kamenarsky


People also ask

Why interface is used instead of multiple inheritance?

We can inherit enormously more classes than Inheritance, if we use Interface. Methods can be defined inside the class in case of Inheritance. Methods cannot be defined inside the class in case of Interface (except by using static and default keywords). It overloads the system if we try to extend a lot of classes.

CAN interface can be inherited?

// within the class. Interface inheritance : An Interface can extend other interface. Inheritance is inheriting the properties of parent class into child class.

CAN interface have inner class in Java?

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.

What is the difference between interface and inheritance in Java?

Summary – Inheritance vs Interface in Java Inheritance and interfaces are related to object-oriented programming. The difference between inheritance and interface is that inheritance is to derive new classes from existing classes and interfaces is to implement abstract classes and multiple inheritance.


1 Answers

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.

like image 129
irreputable Avatar answered Oct 24 '22 05:10

irreputable