Can overridden methods differ in return type?

2019-01-01 05:03发布

Can overridden methods have different return types?

9条回答
千与千寻千般痛.
2楼-- · 2019-01-01 05:33

The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.

查看更多
皆成旧梦
3楼-- · 2019-01-01 05:34

Java supports* covariant return types for overridden methods. This means an overridden method may have a more specific return type. That is, as long as the new return type is assignable to the return type of the method you are overriding, it's allowed.

For example:

class ShapeBuilder {
    ...
    public Shape build() {
    ....
}

class CircleBuilder extends ShapeBuilder{
    ...
    @Override
    public Circle build() {
    ....
}

This is specified in section 8.4.5 of the Java Language Specification:

Return types may vary among methods that override each other if the return types are reference types. The notion of return-type-substitutability supports covariant returns, that is, the specialization of the return type to a subtype.

A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2, if and only if the following conditions hold:

  • If R1 is void then R2 is void.

  • If R1 is a primitive type, then R2 is identical to R1.

  • If R1 is a reference type then:

    • R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9), or

    • R1 = |R2|

("|R2|" refers to the erasure of 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.

查看更多
千与千寻千般痛.
4楼-- · 2019-01-01 05:37

yes It is possible.. returns type can be different only if parent class method return type is
a super type of child class method return type..
means

            class ParentClass {
                  public Circle() method1() {
                    return new Cirlce();
                  }
            }

            class ChildClass extends ParentClass {
                  public Square method1() {
                     return new Square();
               }
            }


            Class Cirlce {

            }

            class Square extends Circle {

            }

        ---

If this is the then different return type can be allowed...

查看更多
只若初见
5楼-- · 2019-01-01 05:39

Yes, if they return a subtype. Here's an example:

package com.sandbox;

public class Sandbox {

    private static class Parent {
        public ParentReturnType run() {
            return new ParentReturnType();
        }
    }

    private static class ParentReturnType {

    }

    private static class Child extends Parent {
        @Override
        public ChildReturnType run() {
            return new ChildReturnType();
        }
    }

    private static class ChildReturnType extends ParentReturnType {
    }
}

This code compiles and runs.

查看更多
有味是清欢
6楼-- · 2019-01-01 05:44

Broadly speaking yes return type of overriding method can be different. But its not straight forward there are some case involved in this.

Case 1: If the return type is primitive data type or void.

Output: If the return type is void or primitive then the data type of parent class method and overriding method should be same. e.g. if return type is int, float, string then it should be same

Case 2: If the return type is derived data type:

Output: If the return type of the parent class method is derived type then the return type of the overriding method is same derived data type of sub class to the derived data type. e.g. Suppose i have a class A B is subclass to A C is subclass to B D is subclass to C then if if the super class is returning type A then the overriding method is subclass can return A, B, C or D type i.e its sub types. This is also called as covarience.

查看更多
姐姐魅力值爆表
7楼-- · 2019-01-01 05:44

YES it can be possible

class base {

 base show(){

System.out.println("base class");

return new base();

}
}

class sub extends base{

sub show(){

    System.out.println("sub class");

    return new sub();

 }
}

class inheritance{

 public static void main(String []args) {

        sub obj=new sub();

            obj.show();
 }
}
查看更多
登录 后发表回答