• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question About "instanceof" operator

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't aswer why i can compile with Runnable and not with cat.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/246243/java-programmer-SCJP/certification/instanceof-interface
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much to all for the replies.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Let's go to the waterfront with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic