Could someone explain why the following code shouldn't compile (it doesn't compile, but I don't see a good reason why it shouldn't)?
The compile error is "types TestB and TestA are incompatible. Both define test() but with unrelated return types". Thats all find and dandy, but I'm overriding test() in TestAB to return TestAB, which is a subtype of both TestA and TestB. In IntelliJ Idea, this doesn't get flagged as an error. However, if I remove the override on test(), it does get flagged with the above message, as I would expect.
Sebastian Janisch wrote:You cannot nest interfaces. Interfaces are top level constructs which means they have a designated file.
Actually, you can nest interfaces just fine. That's not a problem at all.
Tim, you're certainly correct in thinking that your TestAB interface looks as though it should be allowed; it satisfies all the applicable contracts. My best answer as to why this is not allowed is to invoke historical precedent. Covariant return types are a relatively new feature, and the rule that says "an interface can't extend two other interfaces which include conflicting method signatures" is of course older. When covariance was added, they didn't update that rule to read "... unless the programmer can make clever use of covariance to set things right." I think your TestAB interface is rejected before the body is even parsed, according to the no-conflict rule, which still applies in its original form.
Some digging around in the Bug Database reveals that it is indeed a compiler bug that was identified a few years ago but has never been fixed. (http://bugs.sun.com/view_bug.do?bug_id=6294779). The workaround seems to be compiling with a different compiler, apparently it compiles in Eclipse also.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter