Page 1 of 1

About Question enthuware.ocpjp.v21.2.3725 :

Posted: Thu Sep 11, 2025 11:57 am
by giginar
A ac = new C();
if (ac instanceof B b1) {
...
}

- ac → at runtime this is a C object.
- instanceof B → evaluates to true because C is a subclass of B.
- If the test is true, the variable b1 is created and bound to the same object that ac is pointing to, but typed as B.

Hello, is this what happens here?

Re: About Question enthuware.ocpjp.v21.2.3725 :

Posted: Thu Sep 11, 2025 9:10 pm
by admin
Yes, that is correct.

The benefit of getting the variable b1 typed to B is that now you can call methods defined in B on that reference without casting the original reference ac. i.e. instead of doing ((B) ac).b(), you can just do b1.b().