File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question About "instanceof" operator

 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alex Anto Navis Lawrence
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't aswer why i can compile with Runnable and not with cat.
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Paul Michael
Ranch Hand
Posts: 697
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Javier!

you might be interested in this link:

http://mindprod.com/jgloss/instanceof.html

it's a nice intro to the instanceof operator.

hope this helps.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much to all for the replies.
 
Charith Fernando
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic