Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariance problem with multiple interface inheritance

 
Timothy Moose
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot nest interfaces. Interfaces are top level constructs which means they have a designated file.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Timothy Moose
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic