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?
About Question enthuware.ocpjp.v21.2.3725 :
Moderator: admin
-
- Posts: 13
- Joined: Fri Feb 07, 2025 5:36 pm
- Contact:
-
- Site Admin
- Posts: 10419
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v21.2.3725 :
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().
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().
Who is online
Users browsing this forum: No registered users and 20 guests