• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

instansof operator.

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Good find Avishkar! Thanks for the clarification.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



Thanks Dieter,

All the best with your exam.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic