About Question enthuware.ocajp.i.v7.2.1095 :
Moderator: admin
-
- Posts: 9
- Joined: Fri Jan 31, 2014 8:26 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1095 :
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1095 :
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.
int i = 1;
return i;
it will not work.
HTH,
Paul.
-
- Posts: 7
- Joined: Fri Nov 20, 2015 7:03 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1095 :
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?
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1095 :
That is why the correct answer is "Compilation fails".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.
Who is online
Users browsing this forum: No registered users and 10 guests