*
The moose likes Beginning Java and the fly likes Covariance problem with multiple interface inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Covariance problem with multiple interface inheritance" Watch "Covariance problem with multiple interface inheritance" New topic
Author

Covariance problem with multiple interface inheritance

Timothy Moose
Greenhorn

Joined: Aug 11, 2009
Posts: 10
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
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You cannot nest interfaces. Interfaces are top level constructs which means they have a designated file.


JDBCSupport - An easy to use, light-weight JDBC framework -
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ernest Friedman-Hill wrote:Actually, you can nest interfaces just fine. That's not a problem at all.



Totally new for me
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Sebastian Janisch wrote:
Ernest Friedman-Hill wrote:Actually, you can nest interfaces just fine. That's not a problem at all.



Totally new for me

Just like any other non-method, nested interfaces are implicitly public static.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Timothy Moose
Greenhorn

Joined: Aug 11, 2009
Posts: 10
The Idea Eclipse compiler did work, but I'm trying to avoid IDE dependencies and none of the Maven compiler plugins seem to work. Guess I need to rework my APIs. Thanks for the replies.
 
Don't get me started about those stupid light bulbs.
 
subject: Covariance problem with multiple interface inheritance
 
Similar Threads
Inner class and interface
Using super and this in the constructor together
toString is overrides Object Class or TestA