I’d like to use define a generic class in java, that can only be instantiated using my custom data types that all share the same base class. Is it possible to do so without checking the datatype at runtime? Furthermore I’d like to prohibit instantiating a generic class without providing a datatype.
Edit:
I writing a java wrapper for my c++ library and therefore the java class should only allow wrapper classes for my c++ datatypes as parameters. I just tried using bounded type parameters, as suggested, but then I can still instantiate an object like this:
MyClass c = new MyClass();
This is basically the code:
public class MyClass<T extends MyType>
{
public MyClass()
{
}
public doSomething(MyOtherClass<T> other)
{
}
}
What you are looking for is called Bounded Type Parameters: