• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Doubt in instanceof operator

 
Greenhorn
Posts: 25
  • 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, which expression identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?


class A {}
class B extends A {}
class C extends B {}
class D extends A {}

Options:
a) obj instanceof B
b) obj instanceof A && !(obj instanceof C)
c) obj instanceof B && !(obj instanceof C)
d) !(obj instanceof C || obj instanceof D)
e) !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)

I thought the answer was option e) but turned out to be option c).

Can anyone explain.

Thanks in advance

Kayal
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can't be e), because object of class B is instance of class A.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi umashankar,

i think the question means ..

for B b=new B;
then since class B extends class A..then b is instanceof b is true as well
as b is instanceof a is also true ...so i think there could be multiple
answers..
friends do correct me if iam wrong ...

thanks & regards

srikanth
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
obj instanceof B && !(obj instanceof C)

if obj was created instantiating B

obj instanceof B is true
obj instanceof C is false

true && !(false)
true && true
true (Yes obj refers to an Object of B)

now if obj was created instantiating C
obj instanceof B is true // C isa B
obj instanceof C is true

true && !(true)
false
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kayalvizhi,

I must say I definitely agree with Pavel.
The question assumes you have a line �obj = new B()�; where obj is declared as B or A.
So, if obj is of type B, then
  • the test obj instanceof A will always return true (as B extends A),
  • obj instanceof B will return true (obj is of type B),
  • obj instanceof D will return false(B has nothing to do with D)
  • obj instanceof C will return false(as C extends B and obj is of type B)

  • => obj instanceof B && !(obj instance of C)
     
    Ranch Hand
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Umashankar,

    I can remember this Question ,, i did this when i was preparing for SCJP . It is a very good Q which tests whether the candidate knows the mechanics of "instanceof" operator
    Ok. I will explain how the answer was c)

    Answer a) --> wrong ! since obj can be an object created from the class C, and still obj instanceof B returns true !! (Since class C extends Class B)

    Answer b) --> if obj instanceof A evaluates to true, that means the obj can be an object created from any of the classes A,B,C or D . For obj instanceof C to be false , then obj can be an object created from any of the classes A, B or D. so for "obj instanceof A && !(obj instanceof C)" to evaluate to true, then obj can be an object created from either A,B or D. So this answer is also wrong !!

    Answer c) --> if obj instanceof B evaluates to true, then obj can be an object created by either class B or class C. and for "obj instanceof C" to be false, then obj should be an object created by A,B or D . hence, for obj instanceof B && !(obj instanceof C)
    to be true , the object ob must be created from class B


    i think u can follow my argument style similarly for the other options Good luck.

    Cheers
    Mahen
     
    Ranch Hand
    Posts: 657
    Spring VI Editor Clojure
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Santana's got the best explanation so far.

    The question asks for a way to determine if the object was created as a "new B()". Naturally, we'll want to know if "obj instanceof B". But from the class definitions, we can see that C extends B and therefore a new C() object is also an instanceof B. So, we'll need to exclude C objects from our test.

    This leads to the statement "obj instanceof B && !(obj instanceof C)" (answer c).
     
    srikanth reddy
    Ranch Hand
    Posts: 252
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks uma for the nice question ...
    uma for the option e its u cant instantiate it by A,C,D...even with B also ..so the only option is C as explained by santana ....

    thanks & regards

    srikanth
     
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Option e) also wouldn't compile unless obj was declared as A.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic