About Question enthuware.ocajp.i.v7.2.1095 :

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

Moderator: admin

Post Reply
boonnick
Posts: 9
Joined: Fri Jan 31, 2014 8:26 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1095 :

Post by boonnick »

Seeing that public short getValue(){ return 1; } compiles ok in that the literal int gets returned as a short, is it that the return statement acts like an assignment, and there's implicit narrowing?

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

Re: About Question enthuware.ocajp.i.v7.2.1095 :

Post by admin »

Since 1 is a compile time constant and fits into a short, it works fine. If you have something like:
int i = 1;
return i;
it will not work.
HTH,
Paul.

meghajor
Posts: 7
Joined: Fri Nov 20, 2015 7:03 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1095 :

Post by meghajor »

It says "the return type of the overriding method must match exactly to the return type of the overridden method if the return type is a primitive."
The return types for the base and subclass differs here.
The return type byte fits within short. Is that why it invokes Base 2's method?
So the primitives be exact?

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

Re: About Question enthuware.ocajp.i.v7.2.1095 :

Post by admin »

meghajor wrote:It says "the return type of the overriding method must match exactly to the return type of the overridden method if the return type is a primitive."
The return types for the base and subclass differs here.
That is why the correct answer is "Compilation fails".

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests