Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

reference casting from Mugal

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
A bit confused on this question:
Given three classes A, B, C, where B is a sublcass of A and C is a subclass of B, which one of these boolean expressions correctly identifies when an object o has actually been instantiated from class B as opposed to from A or C?
Select the one right answer.
(a) (o instanceof B) && (!(o instanceof A))
(b) (o instanceof B) && (!(o instanceof C))
(c) !((o instanceof A) || (o instanceof B)
(d) (o instanceof B)
(e) (o instanceof B) && !((o instanceof A) || (o instanceof C))
B is given as the correct answer with the following explanation:
The expression (o instanceof B) will return true if the object referred to by o is of type B or a subtype of B. The expression (!(o instance of C)) will return true unless the object referred to by o is of type C or a subtype of C. Thus, the expression (o instanceof B) && (!(o instanceof C)) will only return true if the object is of type B or a subtype of B that is not C or a subtype of C.
--------------------------------
The phrase "true if the object is of type B or a subtype of B that is not C or a subtype of C" in the last sentence of the answer is not clear to me. Didn't the question state C is a subclass of B? So, isn't this in contradiction with the above phrase?
TIA,
Jarrod
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jarrod,
In the question:

Originally posted by Jarrod Legion:

Given three classes A, B, C, where B is a subclass of A and C is a subclass of B, which one of these boolean expressions correctly identifies when an object o has actually been instantiated from class B as opposed to from A or C?


the author is asking to provide an expression which would
prove that an object o referenced by a reference variable of
type A,B,or C, is itself actually an object type B.
So, since instanceof says whether an object is an instance of
a type or of any suptype, given the relationships C extends B,
B extends A, what you would like is a combination of instanceof
expressions that tells you that o is a B and not a C (nor an A).
The phrase "true if the object is of type B or a subtype
of B that is not C or a subtype of C" refers to the result
of the instanceof operator and not to the actual relationship
the question is dealing with.
I hope this helps
Gian Franco
p.s. the reference variable type could actually also be Object,
but it's the object type we are interested in here.
 
Jarrod Legion
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gian,
Thanks for the explanation. Just so that I am understanding what you've just said:
According to the inheritance hierarchy, if we eliminate the instantiation of class C referenced by reference variable type of either Object, A, or B, and as long as o is an instance of B, rest can be assured that object o has actually been instantiated from class B. Is this right?
TIA,
-Jarrod
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
Why is the answer not e ?
o instanceof A is also possible ??
am i missing something ?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vishy,


Why is the answer not e ?


If o is indeed B, the second half of the expression :
!((o instanceof A) || (o instanceof C))
will return false.
Hence, (e) (o instanceof B) && !((o instanceof A) || (o instanceof C)) will actually return false when the object o is indeed a B.
[ January 17, 2004: Message edited by: David Hadiprijanto ]
 
Jarrod Legion
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishy,
Yes, o is instanceof A; however o is not instanceof C. So, for e, if you evaluate the conditional operators, the answer is false.
HTH
Cheers,
-Jarrod
 
reply
    Bookmark Topic Watch Topic
  • New Topic