• 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

JQPlus question on "instanceof"

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following class definitions the following expression :
(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)
correctly indentifies whether the object referred to by obj was created by instantiating class B rather than classes
A,C, D ?
class A
class B extends A {}
class C extends B {}
class C extends D {}
The answer is False
Now is the obj is an instance of class B, the instanceof check
done with C and D should return false.
That is ( obj instanceof C ) = false
( obj instanceof D ) = false
( obj instanceof A ) = true
Then how does the expression evaluate to false ???
Can someone explain ..
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
because it could have been instantiated by new A(), too.
Object obj = new A();
(obj instanceof A) -> true
!(obj instanceof C) --> true
!(obj instanceof D) --> true
hope this helps,
Axel

[This message has been edited by Axel Janssen (edited July 02, 2001).]
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the question states that it "obj was created by instantiating class B "
So still how does the expression evaluate to false ??
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela
The expression, as Axel pointed out, evaluates to true. The Answer to the question is false. Meaning that the code will not tell if the object was instantiated as an instance of B, but not A, C, or D. It will tell you if it was not C or D, but the way it is written it won't tell you if it was A or B, just that it was one of those two.

hope that helps
Dave
 
reply
    Bookmark Topic Watch Topic
  • New Topic