The getValue() method is overridden in two ways. Which one is correct?
<p>My problem with the following example is that I have read that if you override a function, the return type cannot be changed unless you are overriding in a descendant object, in which case you may change the return type from <code>ParentClass</code> to <code>ChildClass</code> but that is not the case in either of these cases. We change the return type from String to Object in the first example, and from Object to String in the second one, neither of which are class A or B. Maybe it is the case that you can return a more specific type in any overridden functions?</p>

<p>I: This one is definitely false because you are returning a more general type when overriding.</p>

<pre><code> public class Test {
  public static void main(String[] args) {
    A a = new A();
    System.out.println(a.getValue());
  }
}

class B {
  public String getValue() {
    return "Any object";
  }
}

class A extends B {
  public Object getValue() {
    return "A string";
  }
}
</code></pre>

<p>II: This must be the correct one but I don't understand because I thought you could only override from returning type B in class B to type A in class A. That that was the only exception and in previous versions of Java, even that wasn't allowed so I don't see how we can change Object to String.</p>

<pre><code>public class Test {
  public static void main(String[] args) {
    A a = new A();
    System.out.println(a.getValue());
  }
}

class B {
  public Object getValue() {
    return "Any object";
  }
}

class A extends B {
  public String getValue() {
    return "A string";
  }
}
</code></pre>