• 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

Object resolution

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
The question is
Does (obj instanceof B) && ! (obj instanceof C)
correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?
The answer is true. I agree that !(obj instanceof C) says that it does not belong to C or it's subclass D
But, how does it say that it does not belong to superclass A as
A is not mentioned in (obj instanceof B) && ! (obj instanceof C)
if the answer was
(obj instanceof B) && ! (obj instanceof C) && ! (obj instanceof A), then it is perfectly clear.
Any help is greatly appreciated.
-Jay
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you had created the object like this
A a = new A();
then the expression would evaluate to false, since A is not a subclass of B, but with the current expression, AS IS, the answer is correct since it correctly identifies an instance of class B and not A because obj is not an instance of C but is an instance of B!
Val
 
reply
    Bookmark Topic Watch Topic
  • New Topic