Newer Version Available
Using the instanceof Keyword
If you need to verify at run time whether an object is actually an instance of a particular class, use the instanceof keyword. The instanceof keyword can only be used to verify if the target type in the expression on the right of the keyword is a viable alternative for the declared type of the expression on the left.
You could add the following check to the Report
class in the classes and
casting example before you cast the item back into a CustomReport
object.
1If (Reports.get(0) instanceof CustomReport) {
2 // Can safely cast it back to a custom report object
3 CustomReport c = (CustomReport) Reports.get(0);
4 } Else {
5 // Do something with the non-custom-report.
6}