About Question enthuware.ocpjp.v7.2.1228 :

Moderator: admin

Post Reply
mchristiaan
Posts: 4
Joined: Sat Aug 08, 2015 8:01 pm
Contact:

About Question enthuware.ocpjp.v7.2.1228 :

Post by mchristiaan » Sat Dec 01, 2018 8:48 am

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.

admin
Site Admin
Posts: 8619
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1228 :

Post by admin » Sat Dec 01, 2018 9:26 am

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.
If you like our products and services, please help us by posting your review here.

mchristiaan
Posts: 4
Joined: Sat Aug 08, 2015 8:01 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1228 :

Post by mchristiaan » Sat Dec 01, 2018 10:35 pm

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?

admin
Site Admin
Posts: 8619
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1228 :

Post by admin » Sat Dec 01, 2018 10:40 pm

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.

Post Reply

Who is online

Users browsing this forum: bkazooie and 1 guest