• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Confusion Related to instanceof operator

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,

I came across a confusion while reading instanceof operator. As per my understanding below code must have thrown a compile time error as Map & Collection are not in same class hierarchy. But code compiles fine please go through the code & let me know why is it happening ?





Thanks & Regards
 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String class is final and cannot be extended. using any class that's final , you get the same compile error
 
Bartender
Posts: 4568
9
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using instanceof will give a compiler error any time there's absolutely no way the check can be true (because it must be an error - why would you check in that case?).

Now, looking at two of the cases you've highlighted, at first glance it would seem they can't be true. But the point here is that Map is an interface, so it is possible to have an object where the checks succeed. Consider these classes:

The same trick doesn't work with String because, as Harsha says, it's final, so you can't subclass it.

If you change the type of m to HashMap, you'll find the Date one fails as well. You can have an object that's a Date and a Map (see above), but you can't have an object that's a Date and a HashMap, as that would need multiple inheritance.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is intelligent enough to know nothing can be instanceOf String except String
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instanceof gives ERROR only when the the class of reference is of different hierarchy
which is he case with Line 16.

the map reference checking the instanceof operator on PEER classes , Which always return false because they are not IN hierarchy.
which is the case with Line 14 and 15

 
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may help you
instanceof
is-a
 
reply
    Bookmark Topic Watch Topic
  • New Topic