Page 1 of 1

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

Posted: Sun Sep 21, 2014 4:24 pm
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?

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

Posted: Sun Sep 21, 2014 8:10 pm
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.

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

Posted: Tue Nov 24, 2015 6:11 pm
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?

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

Posted: Tue Nov 24, 2015 8:23 pm
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".