Whats the java version used in the actual exam?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.
About Question enthuware.ocajp.i.v7.2.1047 :
Moderator: admin
-
- Posts: 2
- Joined: Thu Feb 26, 2015 3:27 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1047 :
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1047 :
OCAJP 7 i.e. 1Z0-803 is based on Java SE 7.
-
- Posts: 1
- Joined: Thu Jul 27, 2017 3:49 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1047 :
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");
}
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");
}
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1047 :
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.
But if you change public List metodo() to public ArrayList metodo(), it will work.
Who is online
Users browsing this forum: No registered users and 25 guests