object cast will be correct namelyit provides an operatorinstanceofthat allows us to test whether an object variable is referring to an object of certain class (or implementing certain interfacethe syntax for using this operator is object referenceinstanceof reference_typewhere object_reference is an expression that evaluates to an object reference and reference_type is the name of some existing classinterfaceor enum (section if object_reference is indeed an instance of reference_typethen the expression above returns true otherwiseit returns false thuswe can avoid classcastexception from being thrown in the code fragment above by modifying it as followsnumber ninteger in new integer( )if ( instanceof integeri (integern/this is legal new double( )if ( instanceof integeri (integern/this will not be attempted casting with interfaces interfaces allow us to enforce that objects implement certain methodsbut using interface variables with concrete objects sometimes requires casting suppose we declare person interface as shown in code fragment note that method equalto of the person interface takes one parameter of type person thuswe can pass an object of any class implementing the person interface to this method code fragment interface person