views:

81

answers:

4

I'm trying to create a new class by subclassing another generic class (with a bound) and implementing a generic interface (without a bound):

public class Foo1<T extends Bar> {
    ...
}

public interface Foo2<T> {
    ...
}

public class ProblemClass<T extends Bar, U> 
        extends Foo1<T extends Bar> implements Foo2<U> {
    ...
}

This gives me compile errors. I also tried:

public class ProblemClass<T, U> 
        extends Foo1<T extends Bar> implements Foo2<U> {
    ...
}

public class ProblemClass<T extends Bar, U> 
        extends Foo1<T> implements Foo2<U> {
    ...
}

But neither of these work either.

What's the correct syntax to define my subclass in a way that lets me keep the typing generic, letting me pass they types along to the superclass and interface? Is this even possible?

Thanks!

+1  A: 

This declaration should work fine. What error do you get? What compiler are you using?

class ProblemClass<T extends Bar, U>
  extends Foo1<T>
  implements Foo2<U>
{
  ...
}

This is valid Java. If IDEA's compiler rejects it, IntelliJ has a bug.

erickson
I'm using IntelliJ IDEA 9, the error in this case is:Type parameter 'T' is not within it's bound; should implement Bar
Pinkerton
+1 for the first Idea bug hint
Arne Burmeister
+1  A: 

From the code you posted, you only have class Foo not Foo1. Is that the reason? or just posting-edit error?

I think this should work

public class ProblemClass<T extends Bar, U> 
        extends Foo1<T> implements Foo2<U> {
    ...
}
Fadrian Sudaman
Sorry, I must have accidentally changed `Foo1` to `Foo` when I edited the question for formatting. The original question contained `Foo1`.
William Brendel
I just tested this and it compiled for me. @Pinkerton, if this doesn't compile for you, please post the error message.
Joe Carnahan
@Joe The error I'm seeing (in IntelliJ IDEA 9) is:Type parameter 'T' is not within its bound; should implement 'Bar'
Pinkerton
+1  A: 

This gives no compile errors with JDK 1.6.0_07

public class Bar {}

public class Foo1<T extends Bar> {}

public interface Foo2<T> {}

public class ProblemClass<T extends Bar, U> extends Foo1<T> implements Foo2<U> {}
ahmadabdolkader
For some reason, IntelliJ IDEA 9 is complaining with:Type parameter 'T' is not within its bound; should implement 'Bar'
Pinkerton
A: 

This (from your question):

public class ProblemClass<T extends Bar, U> 
        extends Foo1<T extends Bar> implements Foo2<U> {
    ...
}

should be this:

public class ProblemClass<T extends Bar, U> 
        extends Foo1<T> implements Foo2<U> {
    ...
}

That is, you should not restate the bounds when extending Foo1.

With that correction the code compiles fine in javac. If IntelliJ IDEA still refuses to compile it then you may have found a bug, or perhaps the "Bar" you're referring to in Foo.java is not the same "Bar" in ProblemClass.java?

Laurence Gonsalves
Yeah, I'm thinking I may have found a bug in IDEA. The simplified cases I've tried out work just fine with the syntax you've proposed.
Pinkerton