aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using instanceof operator" Watch "Using instanceof operator" New topic
Author

Using instanceof operator

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
interface Face { }

class Bar implements Face { }

class Foo extends Bar { }

Results when using the instanceof operator :

Foo [] instanceof (Foo , Bar , Face ) --- False

Foo[1] instanceof (Foo, Bar , Face , Object ) --- True

Can some one please explain the above ?
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,



It is because, any Foo object is an instance of Foo.
Foo is an instance of Bar, because Foo extends Bar.
Foo is an instance of Face because Class Bar implements Face and Bar is a direct super-class of Foo, so Foo is an instance of Face as well.
Any class in Java is an instance of Object.

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
John Mercier
Greenhorn

Joined: Nov 23, 2006
Posts: 9

Also, Foo[] is an Array not a Foo.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Jose,

Additionally, when you ask some question, please separate it from comments. Else hard time to understand your question.

Please check this out



Foo[] is an array and an array is only an instance of Object.
Foo[1] is talking about a specific object.

Hope this will help you clear your doubt.

Happy Preparation,
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Foo [] instanceof (Foo , Bar , Face ) --- False


This will cause compilation error(if written as Java code) as Foo or Bar or Face isn't supertype of Foo[]. (i.e they don't fall under same hierarchy).


SCJP6
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Thanks all for responding.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91


It is because we have made Foo[] as an array object


Thanks & Regards
Sumit Kothalikar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using instanceof operator