Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about instanceof operator....

 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it valid to use this with interfaces?

Will (someObject instanceof someInterface) return true if someObject implements SomeInterface???

What about (superInterface instanceof childInterface)?

Will that return true if the object superInterface which is Identified as implementing SuperInterface and is actually implementing ChildInterface which extends SuperInterface.

I hope that made sense.

-Tad
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your first question, and the answer is "yes", instanceof does the right thing if you ask if an object's class implements a given interface.

Your second question is harder to follow, so I will let you answer it for yourself, armed with the knowledge that instanceof X will return true whenever a cast to X would succeed.
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I was writing that I came to the conclusion that if the answer to the first question is yes then the answer to the second should be yes too as its the same question (kind of).


-Tad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic