Page 1 of 1
About Question enthuware.ocpjp.v7.2.1228 :
Posted: Sat Dec 01, 2018 8:48 am
by mchristiaan
if obj instanceof B is true; this means that obj is also instanceof A right? Please help me understand the correct expression (obj instance B) && !(obj instanceof C) in the Explanation part.
Re: About Question enthuware.ocpjp.v7.2.1228 :
Posted: Sat Dec 01, 2018 9:26 am
by admin
There is a detailed explanation given with this question. Can you please go through it and specify which part is not clear so that I can clear your doubt?
Paul.
Re: About Question enthuware.ocpjp.v7.2.1228 :
Posted: Sat Dec 01, 2018 10:35 pm
by mchristiaan
Paul
The explanation states "The given expression will not be able to distinguish between an object of class A and an object of class B. It will return true in both the cases........Correct expression would be (obj instanceof B) && ! (obj instanceof C). This will return true only if obj points to an object of class B and not of A, C, or D. --> Shouldn't the last sentence be: This will return true only if obj points to an object of class B and not of C, or D. Because if obj is instance of B means it's also instance of A since B extends A?
Re: About Question enthuware.ocpjp.v7.2.1228 :
Posted: Sat Dec 01, 2018 10:40 pm
by admin
Yes, an instance of B is also an instance of A. But an instance of A doesn't mean it is an instance of B. So if obj points to an instance of A, then "obj instanceof B" will be false. Therefore, the expression will return false in the case of A also and not just C and D.