This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference casting from Mugal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reference casting from Mugal" Watch "reference casting from Mugal" New topic
Author

reference casting from Mugal

Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
Posts: 18
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
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
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.


"Eppur si muove!"
Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
Posts: 18
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
Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hi there,
Why is the answer not e ?
o instanceof A is also possible ??
am i missing something ?


"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
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

Joined: Dec 24, 2002
Posts: 18
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reference casting from Mugal
 
Similar Threads
Khalid Mughal,for jdk 1.4, page 271, Question 6.23, wrong answer. Correct Me.
KAM India Edition Question 6.22
Question from khalid mughal...
Khalid's Q6.23 pg 271
instanceof operator