File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about instanceof operator...." Watch "Question about instanceof operator...." New topic

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.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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).

I agree. Here's the link:
subject: Question about instanceof operator....
It's not a secret anymore!