Can overridden methods have different return types?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java supports* covariant return types when overriding methods. An overriding method may have a more specific return type than the method being overridden. That is, as long as the new method’s return type is assignable to the return type of the method it overrides, it’s allowed.
For example:
This is specified in section 8.4.5 of the Java Language Specification:
("|R2|" refers to the erasure of the type R2, as defined in §4.6 of the JLS.)
* Prior to Java 5, Java had invariant return types, which meant the return type of a method override needed to exactly match the method being overridden.