Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ambiguous methods differ only by (generic) return type
ambiguous methods differ only by (generic) return type [message #634753] Sat, 23 October 2010 05:12 Go to next message
Brian Vosburgh is currently offline Brian VosburghFriend
Messages: 137
Registered: July 2009
Senior Member
I have the following interfaces:
public interface Bar {
	String getSomething();
}
public interface SubBar extends Bar {
	String getSomethingElse();
}

public interface Foo {
	<T extends Bar> Iterable<T> bars();
}
public interface SubFoo extends Foo {
	<T extends SubBar> Iterable<T> bars();
}

I'm not sure whether SubFoo's declaration of bars() should be allowed, but the compiler seems to have no complaint. But that may be because it does not detect the "override"; since the following code does not compile:
public void test(SubFoo1 subFoo) {
	for (SubBar subBar : subFoo.bars()) {
		System.out.println(subBar);
	}
}

The compiler complains that the clause "subFoo.bars()" is ambiguous - it's not sure whether to invoke Foo.bars() or SubFoo.bars().

Can anyone explain to me why the compiler does not detect the "override"? (Additionally, if I try to add an @Override annotation to the SubFoo.bars() declaration, I get yet another compile error.) It seems to me either the SubFoo.bars() declaration should be disallowed or the call to SubFoo.bars() is unambiguous.

Thanks.
Brian
Re: ambiguous methods differ only by (generic) return type [message #634867 is a reply to message #634753] Sun, 24 October 2010 17:53 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Brian Vosburgh wrote on Sat, 23 October 2010 07:12
I have the following interfaces:
public interface Bar {
	String getSomething();
}
public interface SubBar extends Bar {
	String getSomethingElse();
}

public interface Foo {
	<T extends Bar> Iterable<T> bars();
}
public interface SubFoo extends Foo {
	<T extends SubBar> Iterable<T> bars();
}

I'm not sure whether SubFoo's declaration of bars() should be allowed, but the compiler seems to have no complaint.



IMHO it should complain "The return type is incompatible with Foo.bars(int)",
essentially because Iterable<SubBar> is not conform to Iterable<Bar>.

Anyway, the compiler's behavior doesn't seem kosher to me,
so I suggest you file a bug where we can further track this issue.


thanks,
Stephan

PS:
As for your example, may you need this:
public interface Foo {
        Iterable<? extends Bar> bars();
}
public interface SubFoo extends Foo {
        @Override
        Iterable<? extends SubBar> bars();
}

Re: ambiguous methods differ only by (generic) return type [message #635214 is a reply to message #634867] Tue, 26 October 2010 02:33 Go to previous message
Brian Vosburgh is currently offline Brian VosburghFriend
Messages: 137
Registered: July 2009
Senior Member
Stephan Herrmann wrote on Sun, 24 October 2010 13:53
Brian Vosburgh wrote on Sat, 23 October 2010 07:12
I have the following interfaces:
public interface Bar {
	String getSomething();
}
public interface SubBar extends Bar {
	String getSomethingElse();
}

public interface Foo {
	<T extends Bar> Iterable<T> bars();
}
public interface SubFoo extends Foo {
	<T extends SubBar> Iterable<T> bars();
}

I'm not sure whether SubFoo's declaration of bars() should be allowed, but the compiler seems to have no complaint.



IMHO it should complain "The return type is incompatible with Foo.bars(int)",
essentially because Iterable<SubBar> is not conform to Iterable<Bar>.

Anyway, the compiler's behavior doesn't seem kosher to me,
so I suggest you file a bug where we can further track this issue.


thanks,
Stephan

PS:
As for your example, may you need this:
public interface Foo {
        Iterable<? extends Bar> bars();
}
public interface SubFoo extends Foo {
        @Override
        Iterable<? extends SubBar> bars();
}




I have entered a bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=328669

As to your suggested code: That type of code is helpful in some places but not so much in others. :-) Also, the compiler complains about the @Override annotation:

Multiple markers at this line
- The method bars() of type SubFoo must override a superclass method
- implements Foo.bars

Pretty funny pair of contradicting messages. :-)

Anyway, if I remove the @Override annotation, the new code compiles fine.

Thanks,
Brian
Previous Topic:Refreshing external folders/resources
Next Topic:How to run Web project in Helios and genimade
Goto Forum:
  


Current Time: Fri Apr 19 02:18:04 GMT 2024

Powered by FUDForum. Page generated in 0.01877 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top