instanceof can be used to test if an object is a direct or descended instance of a given class. instanceof can also be used with interfaces even though interfaces can’t be instantiated like classes. Can anyone explain how instanceof works?
instanceof can be used to test if an object is a direct or descended
Share
First of all, we can store
instancesof classes that implements a particularinterfacein aninterface reference variablelike this.ie, any runtime instance that implements a particular interface will pass the
instanceoftestEDIT
and the output
@RohitJain
You can create instances of interfaces by using anonymous inner classes like this
and you test the instance is of type interface, using
instanceofoperator like thisand the result is ‘true’