Java:InstanceOf
lastmod 2020-01-15created 2020-01-151 edits
Explanation of the JAVA instanceof operator
If the result of an instanceof operation for some type is true, it means a cast to the checked type is possible.
- If the reference variable can use more members than the instance has, it cannot be used.
- If the reference variable can use fewer members than the instance has, it can be used. (Equal is also O).
- Therefore, an ancestor's reference variable can refer to instances of all its descendants.
- ancestor reference variable instanceof descendant instance (always O).
- Object o instanceof descendant instance (always O).
- If you create an empty descendant Class as in example No. , descendant reference variable instanceof ancestor instance (possible O).