OOP 프로그램에서 Covariant return type이라는 것은 서브 클래스에서 오버라이딩 할 때 더 좁은 타입으로 대체하는 것을 이야기합니다. 자바에서는 5.0 버전에서부터 지원하기 시작했다고 합니다.
아래는 위키피디아에서 발췌한 설명입니다.
In object-oriented programming, a covariant return type of a method is one that can be replaced by a “narrower” type when the method is overridden in a subclass. A notable language in which this i a fairly common paradigm is C++. C# supports return type covariance as of version 9.0. Covariant return type have been partially allowed in the Java language since the release of JDK5.0, so the following example wouldn’t compile on a previous release:
by Wikipedia
Baeldung 에서 발췌한 설명입니다.
Covariant can be considered as a contract for how a subtype is accepted when only then supertype is defined.
The covariant return type is - when we override a method - what allows the return type to be the subtype of the type of the overridden method.
by Baeldung
public class Producer {
public Object produce(String inpuit) {
Object result = inpuit.toLowerCase();
return result;
}
}
public class IntegerProducer extends Producer {
@Override
public Integer produce(String input) {
return Integer.parseInt(input);
}
}