Covariant return type in Java

The following code uses the concept of method overrides in Java.

package pkg; import java.util.ArrayList; import java.util.List; abstract class SuperClass { abstract public List<String>getList(); } final class SubClass extends SuperClass { private List<String>list=null; @Override public ArrayList<String> getList() { list=new ArrayList<String>(); list.add("A"); list.add("B"); return (ArrayList<String>) list; } } final public class Main { public static void main(String[] args) { SuperClass s=new SubClass(); List<String>list=s.getList(); for(String str:list) { System.out.println(str); } } } 

By convention, method overrides use the same signature (with return type) in the superclass and subclass. In the above code, the return type of the getList() method in SuperClass is List , and in its subclass, the return type is ArrayList . How does the override method work here?

By the way, it is obvious that ArrayList is an implementation of the List interface, but how does the compiler consider the return type here, overriding the getList() method?

Should I consider something like this ... The return type of an overridden method is allowed as a subtype of the return type of an overridden method.

+6
source share
3 answers

Yes.

At the beginning of Java, this was not the case, but was changed to Java 5.0.

You cannot have two methods in the same class with signatures that differ only in return type. Prior to J2SE 5.0, it was also true that the class could not override the return type of methods that it inherits from the superclass. In this tip, you'll learn about a new feature in J2SE 5.0 that allows covariant return types. This means that a method in a subclass can return an object whose type is a subclass of the type returned by a method with the same signature in the superclass. This feature eliminates the need for excessive type checking and casting.

The source of this information is no longer available at intervets.

+19
source

In object-oriented programming, the covariant type of the return method is a method that can be replaced by a narrower type when overriding a method in a subclass. A notable language in which this is a fairly common paradigm is C ++. Covariant return types have been (partially) allowed in Java since the release of JDK5.0, so the following example will not compile in the previous release:

  // Classes used as return types: class A { } class B extends A { } // "Class B is more narrow than class A" // Classes demonstrating method overriding: class C { A getFoo() { return new A(); } } class D extends C { B getFoo() { return new B(); } } 

More specifically, a covariant (wide or narrower) or contravariant (narrow and wider) return type refers to a situation where the return type of an overriding method changes to a type related to the (but different from) return type of the original overridden method.

The relationship between the two covariant return types is usually such that it allows substituting one type for the other, following the Liskov signature principle.

This usually means that the return types of the overriding methods will be subtypes of the return type of the overridden method. The above example specifically illustrates such a case. If the substitution is invalid, the return type is invariant and causes a compilation error.

Link: https://en.wikipedia.org/wiki/Covariant_return_type

+4
source

Yes, that's right. Since ArrayList is a List, you can return an ArrayList when the original method returned List.

+3
source

Source: https://habr.com/ru/post/913099/


All Articles