File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question About 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 "Question About "instanceof" operator" Watch "Question About "instanceof" operator" New topic

Question About "instanceof" operator

Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
It is said in order to use the "instanceof" operator the object you are checking out must belong to the same hierarchy to the class on the right.

Can somebody explain why Runnable and Javier are in the same hierarchy???. Ok, they extend both Object. Doesn't Cat extend Object too?

Thank You.
[ August 04, 2006: Message edited by: Javier Sanchez Cerrillo ]

SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Alex Anto Navis Lawrence

Joined: Aug 04, 2006
Posts: 2
class Cat { }

class Javier {
public static void main(String [] args) {
Javier d = new Javier();
System.out.println(d instanceof Runnable);

//dont compile why???System.out.println(d instanceof Cat);

Hi ,
The Class Cat and Javier has no relation with one another ,

an reference can be of its own type or its Parent type ,,,,,,,,,, not like it can't be refer to the other childs of the Parent.........

parent - > child1
parent - > child2

so it can be like d is not a refernce of the Cat......... since it doesnt what is it.......has no relation between the classes of the same heriarchy......

since only Parent - child relations only makes some meaning........

try extending the
class Javier extends Cat {
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
It doesn't aswer why i can compile with Runnable and not with cat.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

how "System.out.println(d instanceof Runnable);" works
class Javier extends Object but neither Object nor Javier class does implements runnable interface so where this interface is implemented.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
hi Javier!

you might be interested in this link:

it's a nice intro to the instanceof operator.

hope this helps.

SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Thank you very much to all for the replies.
Charith Fernando
Ranch Hand

Joined: Sep 12, 2005
Posts: 67
hmmm... guess i learnt something new...

so the conclusion would be... if your check whether a particullar object is an instance of an interface, there is no way of checking whether they're in the same hieratchy at compile time rite? but this has to be detected at run time. because

at compile time a is an A but at runtime a would be a D therefore at runtime it realises that its in the same hierarchy and the condition is true...

am i correct in this description?

Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
I agree. Here's the link:
subject: Question About "instanceof" operator
It's not a secret anymore!