Using the instanceof Keyword

If you need to verify at runtime 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.
If (Reports.get(0) instanceof CustomReport) {
    // Can safely cast it back to a custom report object
   CustomReport c = (CustomReport) Reports.get(0);
   } Else {
   // Do something with the non-custom-report.
© Copyright 2000–2014, inc. All rights reserved.
Various trademarks held by their respective owners.