About Question enthuware.ocajp.i.v8.2.1458 :

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

Moderator: admin

Post Reply
FanaticuS
Posts: 2
Joined: Sat Dec 07, 2019 10:19 am
Contact:

About Question enthuware.ocajp.i.v8.2.1458 :

Post by FanaticuS »

Hi,

I have a question about option 5 and it's explanation:

5)
ArrayList<Vehicle> al5 = new ArrayList<>();
Drivable d2 = al5.get(0);

Explanation:
"Since a Vehicle is not a Drivable, you cannot assign an instance of a Vehicle to variable of type Drivable without a cast"

If Vehicle isn't connected to Drivable then how can we assign an instance of a Vehicle to variable of type Drivable with a cast? Or there is a small error in the explanation?

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

Re: About Question enthuware.ocajp.i.v8.2.1458 :

Post by admin »

No, the explanation is correct.
Drivable is an interface. Although Vehicle does not implement Drivable, it is possible for a subclass of Vehicle to implement Drivable. The compiler knows that a variable of type Vehicle could actually point to a Drivable object. (In this case, the list may contain such objects) That is why it allows the assignment with a cast.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: mcpunjabi and 48 guests