• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

instansof operator.

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I have read that instanceof operator can not be used on the instances/classes that belong to different inheritance tree and that it will result in compile error. But now I have come across this example:

And as I look in the book HashMap and Collection are in completly different inheritance tree but the above code still works. Can anyone explain why?

Cheers,
Jan.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Collection is an interface, not a class.
Interfaces are different from classes - they are not in the class inheritance tree, there are 'oudside' of this tree.
Where an interface is on the right side of instanceof operator, the operator tests if given object implements that interface.
Virtually all classes can implement any interface, and compiler cannot reject this test at compile time.


Look at this example:

Here we have:
1. interface Collectionnn
2. two clasess: HashMappp, HashMappp_Collection in one 'inheritance subtree'
3. third class: OtherClass that is in the other 'inheritance subtree' than classes from point 2.
HashMappp doesn't implement Collectionnn, but two other classes do.
 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you for the answer and a great example! Yeah, that's straightforward now. A class can implement any interface so instanceof operator can not reject it !
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think better explanation is that a subclass of the given class can implement the given interface hence the compiler creates no issues.

If you make the class final & there is no implementation of the interface in the hierarchy it will throw an compilation error.

E.g 1 will compile but Eg. 2 wont

1.


2.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good find Avishkar! Thanks for the clarification.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:Good find Avishkar! Thanks for the clarification.



Thanks Dieter,

All the best with your exam.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic