tags:

views:

51

answers:

4

For example:

class MyClass<T extends MyClass2> {
    // Do stuff...
} 

Then later:

MyClass<MyClass2> myClass = new MyClass<MyClass2>();

Does this work? My coworker's hunch is no, but I can't find anything to confirm that for me and the documentation suggests perhaps.

+2  A: 

I don't see anything wrong. Even MyClass<MyClass> m = new MyClass<MyClass>(); should be valid expression (even useful, maybe).

anthares
does myclass extend myclass2?
Jimmy
That's the example...
anthares
Actually no, MyClass doesn't extend MyClass2, T extends MyClass2, it's a bound parameter.
Daniel Bingham
+2  A: 

Yep, that works just fine. Lower bounds are inclusive.

noah
+4  A: 

This works fine. I just wrote this:

public class MoreGeneric {
  public static void main(String[] args) {
    new MyClass1<MyClass2>();
   }


  public static class MyClass1<T extends MyClass2>{}
  public static class MyClass2{}
}

And it compiled fine.

Kylar
+2  A: 

Yes, you can. T extends ClassX checks that ClassX.isAssignableFrom(T.class)).

super is the opossite, so you can use the bound class too.

And... you could program a test to find out :)

helios
Did just that after I posted the question. ;)
Daniel Bingham