Suppose I have below code snippet with JDK 1.7. Using Generics, I am getting confused how to achieve a generic parameterized type with exclusive, not inclusive bounds with using extends
. For example:
abstract class BaseAbstract {
}
class ChildWhichExtendsBaseAbstract extends BaseAbstract {
}
class SomeOtherClassWhichUseGenerics<T extends BaseAbstract> {
}
public class Test {
public static void main(String[] args) {
/**
* I just don't want JVM to compile below line, because I want to use
* generic type with {@link SomeOtherClassWhichUseGenerics} which must
* not be {@link BaseAbstract}
*/
SomeOtherClassWhichUseGenerics<BaseAbstract> obj1 =
new SomeOtherClassWhichUseGenerics<BaseAbstract>();
/**
* I just want JVM to compile below line, because I want to use generic
* type with {@link SomeOtherClassWhichUseGenerics} which must be only
* subclasses of BaseAbstract
*/
SomeOtherClassWhichUseGenerics<ChildWhichExtendsBaseAbstract> obj2 =
new SomeOtherClassWhichUseGenerics<ChildWhichExtendsBaseAbstract>();
}
}
I think this should be one of the known problems/issues/requirements. Is there a work around possible?