I am new to Java and would like to know a bit more. I have a current problem that I would like an answer to, but I would also like to know what the technique is referred to as so that I can do some further reading.
I currently have something like this:
public class BasicActivityExtension {
public Boolean basicExtensionMethod() { }
...
}
public class MyExtension extends BasicActivityExtension {
public Boolean myExtensionMethod() { }
...
}
Then in a service method I have (ignore the ( I couldnt work out how to get the < to stay in this):
public Class < ? extends BasicActivityExtension> getExtensionByActivity(
BasicActivity activity,
ExtensionTypes type)
throws Exception {
...
}
My Question is why does this give an error and what is the name for this technique (when done correctly!)
MyExtension members = (MyExtension) activityService.getExtensionByActivity(activity,
ExtensionTypes.member);
The error is
Cannot cast from Class to MyExtension;
Also if I have an object Class< ? extends BasicActivityExtension> how can I just call the generic methods in BasicActivityExtension without caring what class it is?
It appears you want
When you specify
Classit has to be a class object, not an instance of a class.BTW: I would avoid blindly throwing
ExceptionThere are better ways of handling checked exceptions.