Page 1 of 1

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

Posted: Mon Mar 02, 2015 4:41 am
by rbartolay
This would have been true prior to Java 1.5. But from Java 1.5, an overriding method is allowed to change the return type to any subclass of the original return type, also known as covariant return type. This does not apply to primitives, in which case, the return type of the overriding method must match exactly to the return type of the overridden method.
Whats the java version used in the actual exam?

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

Posted: Mon Mar 02, 2015 4:56 am
by admin
OCAJP 7 i.e. 1Z0-803 is based on Java SE 7.

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

Posted: Thu Jul 27, 2017 3:55 pm
by Molinox
I did understand this question yet, sorry.

For example.
class Base{
public void metodo() {
System.out.println("original");
}
}

public class Child extends Base {
@Override
public void metodo() {//If put here public int metodo, with return type int too, do will not work
System.out.println("changed");
}

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

Posted: Thu Jul 27, 2017 10:44 pm
by admin
As the explanation says, this does not apply to primitives. You can change the return type in the overriding method to a subclass. int is not a subclass of void.
But if you change public List metodo() to public ArrayList metodo(), it will work.