wood burning stoves 2.0*
The moose likes Java in General and the fly likes Question about instanceof operator.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about instanceof operator...." Watch "Question about instanceof operator...." New topic
Author

Question about instanceof operator....

Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
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
 
Consider Paul's rocket mass heater.
 
subject: Question about instanceof operator....