What am I doing wrong with Java generics in this interface and method?

Answer changed to reflect clarification in question The complilation error (type mismatch) is actually perfectly correct. Note that the method definition says the parameter is a List>()) Mixing generics and polymorphism quickly leads to very complex scenarios, so it should be done sparingly.

Answer changed to reflect clarification in question The complilation error (type mismatch) is actually perfectly correct. Note that the method definition says the parameter is a List>. This means that the list can contain any Foo, and the method must even be able to add any object that implements Foo.

And that's not the case when you give it a List, because that is only allowed to contain instances of FooImpl. This works: doAnotherThing(new ArrayList>()); Mixing generics and polymorphism quickly leads to very complex scenarios, so it should be done sparingly.

Michael, this should be fixed. Please see question again. Thanks.

– Polaris878 Jan 4 at 22:41 @Polaris878: changed answer, I think I have it now. – Michael Borgwardt Jan 4 at 23:17.

Works for me. I suspect whatever's causing the error, it isn't in the version you've got up above. Does the code below (all one file, FooBarBaz.

Java) compile for you? The only change I had to make to your code was to make FooImpl.doSomething() public. (Oh, and make it return something.

:)) public class FooBarBaz { T doAnotherThing(Foo foo) { return foo.doSomething(); } public static void main(String args) { new FooBarBaz(). DoAnotherThing(new FooImpl()); } } abstract class Bar { // some neat stuff } class BarImpl extends Bar { // some cool stuff } interface Foo { T doSomething(); } class FooImpl implements Foo { public BarImpl doSomething() { return null; } } Works fine for me with IDEA 9, JDK 1.6 on Mac OS 10.6.5.

You are correct, this does indeed work. However I missed a (major) detail which I highlighted above. – Polaris878 Jan 4 at 22:40.

Let's assume BarImpl extends Bar, which I believe you meant from the beginning. How does the augmented Foo interface look like? Is it: interface Foo { T doSomething(); T doAnotherThing(Foo foo); }?

In such a case everything works with the following impl: class FooImpl implements Foo { public BarImpl doSomething() { return null; } public BarImpl doAnotherThing(Foo foo) { return null; } } Now, there might be a problem if you defined it otherwise: interface Foo { T doSomething(); T doAnotherThing(Foo foo); } Because such a way of defining doAnotherThing introduces another generics parameter. What's confusing the interface's parameter and one from method share the name, i.e. T.(BTW, I was surprised to learn Java allows such confusing name clash) The last definition could be replaced with: interface Foo { T doSomething(); Y doAnotherThing(Foo foo); } which makes it more clear why public BarImpl doAnotherThing(Foo foo) is not a proper way of overriding of this method.

DoAnotherThing is not part of Foo, it is part of another class – Polaris878 Jan 4 at 21:49 I see. My answer was a blind guess. – Grzegorz Oledzki Jan 4 at 23:33.

I believe my original answer is incorrect. The following seems to work fine though. Just make the doSomething method public and it compiles fine under Java6.

Abstract class Bar { // some neat stuff } class BarImpl extends Bar { // some cool stuff } interface Foo { public T doSomething(); } class FooImpl implements Foo { public BarImpl doSomething() { return null; } } public class Test { T doAnotherThing(Foo foo) { return foo.doSomething(); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions