I want to make a generic class of this form:
class MyGenericClass {}
Problem is, I want to be acceptable for T to b
The answer is no. At least there is no way to do it using generic types. I would recommend a combination of generics and factory methods to do what you want.
class MyGenericClass {
public static MyGenericClass newInstance(Long value) {
return new MyGenericClass(value);
}
public static MyGenericClass newInstance(Integer value) {
return new MyGenericClass(value);
}
// hide constructor so you have to use factory methods
private MyGenericClass(T value) {
// implement the constructor
}
// ... implement the class
public void frob(T number) {
// do something with T
}
}
This ensures that only MyGenericClass and MyGenericClass instances can be created. Though you can still declare an variable of type MyGenericClass it will just have to be null.