About Question enthuware.ocpjp.v7.2.1228 :
Moderator: admin
-
- Posts: 4
- Joined: Sat Aug 08, 2015 8:01 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1228 :
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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1228 :
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.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sat Aug 08, 2015 8:01 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1228 :
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?
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?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1228 :
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 42 guests