About Question enthuware.ocpjp.v21.2.3725 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
giginar
Posts: 13
Joined: Fri Feb 07, 2025 5:36 pm
Contact:

About Question enthuware.ocpjp.v21.2.3725 :

Post 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?

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

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

Post 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().

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests