• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about instanceof operator

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following piece of code .


o instanceof Moof should give result as false .Because "two" which is an objet of Moof is now assigned to Object o . So now o is the object of type Object . And Object is the parent class of all the classes so ,
Based on the above o instanceof Moof should be false ? Am i right ?
Thanks
[ December 19, 2007: Message edited by: Manishk Rai ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you assign an object of type Moof to the Object o, you don't really change the type of the actual object, you change just the type of a variable that refers to it. The real type of the object remains the same. The ability to refer to the subclass object using a superclass variable is the essense of polymorphism.

But how to define the real type of the object to which the "o" variable is referring? That's where you use the instanceof operator. During runtime, it checks that the actual type of the object is Moof, and as it is, the operator returns true.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes sir you are absolutly rite. as now o is an instance of Object and it is a superclass of moof so o can't be instance of moof
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

unfortunately You are wrong. The reference o of the type Object can polymorphically refer to any subtype ob the class Object. And if you pass a Moof instance to the equals() method you will get a reference of type Object, which refers to the Moof instance on the heap and then o instanceOf Moof will be true.
 
Manishk Rai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks serge
 
Amit Kathpal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you serg for clearing my doubt
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic