Can overridden methods differ in return type?

前端 未结 12 1533
礼貌的吻别
礼貌的吻别 2020-11-22 16:11

Can overridden methods have different return types?

相关标签:
12条回答
  • 2020-11-22 16:46

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

    0 讨论(0)
  • 2020-11-22 16:47

    well, the answer is yes... AND NO.

    depends on the question. everybody here answered regarding Java >= 5, and some mentioned that Java < 5 does not feature covariant return types.

    actually, the Java language spec >= 5 supports it, but the Java runtime does not. in particular, the JVM was not updated to support covariant return types.

    in what was seen then as a "clever" move but ended up being one of the worst design decisions in Java's history, Java 5 implemented a bunch of new language features without modifying the JVM or the classfile spec at all. instead all features were implemented with trickery in javac: the compiler generates/uses plain classes for nested/inner classes, type erasure and casts for generics, synthetic accessors for nested/inner class private "friendship", synthetic instance fields for outer 'this' pointers, synthetic static fields for '.class' literals, etc, etc.

    and covariant return types is yet more syntactic sugar added by javac.

    for example, when compiling this:

    class Base {
      Object get() { return null; }
    }
    
    class Derived extends Base {
      @Override
      @SomeAnnotation
      Integer get() { return null; }
    }
    

    javac will output two get methods in the Derived class:

    Integer Integer:Derived:get() { return null; }
    synthetic bridge Object Object:Derived:get() { return Integer:Derived:get(); }
    
    

    the generated bridge method (marked synthetic and bridge in bytecode) is what actually overrides Object:Base:get() because, to the JVM, methods with different return types are completely independent and cannot override each other. to provide the expected behavior, the bridge simply calls your "real" method. in the example above, javac will annotate both bridge and real methods in Derived with @SomeAnnotation.

    note that you cannot hand-code this solution in Java < 5, because bridge and real methods only differ in return type and thus they cannot coexist in a Java program. but in the JVM world, method return types are part of the method signature (just like their arguments) and so the two methods named the same and taking the same arguments are nonetheless seen as completely independent by the JVM due to their differing return types, and can coexist.

    (BTW, the types of fields are similarly part of the field signature in bytecode, so it is legal to have several fields of different types but named the same within a single bytecode class.)

    so to answer your question fully: the JVM does not support covariant return types, but javac >= 5 fakes it at compile time with a coating of sweet syntactic sugar.

    0 讨论(0)
  • 2020-11-22 16:50

    Overriding and Return Types, and Covariant Returns
    the subclass must define a method that matches the inherited version exactly. Or, as of Java 5, you're allowed to change the return type in the

    sample code


                                                                                                                class Alpha {
              Alpha doStuff(char c) {
                      return new Alpha();
                  }
               }
                 class Beta extends Alpha {
                        Beta doStuff(char c) { // legal override in Java 1.5
                        return new Beta();
                        }
                 } } 
    As of Java 5, this code will compile. If you were to attempt to compile this code with a 1.4 compiler will say attempting to use incompatible return type – sandeep1987 1 min ago

    0 讨论(0)
  • 2020-11-22 16:51

    Yes it may differ but their are some limitations.

    Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.

    0 讨论(0)
  • 2020-11-22 16:52

    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 Circle {
    
    }
    
    class Square extends Circle {
    
    }
    


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

    0 讨论(0)
  • 2020-11-22 16:52

    Yes. It is possible for overridden methods to have different return type .

    But the limitations are that the overridden method must have a return type that is more specific type of the return type of the actual method.

    All the answers have given examples of the overridden method to have a return type which is a subclass of the return type of the actual method.

    For example :

    public class Foo{
    
       //method which returns Foo
      Foo getFoo(){
          //your code         
      }
    
    }
    
     public class subFoo extends Foo{
    
      //Overridden method which returns subclass of Foo
      @Override
      subFoo getFoo(){
          //your code         
      }
    
    }
    

    But this is not only limited to subclass.Even classes that implement an interface are a specific type of the interface and thus can be a return type where the interface is expected.

    For example :

    public interface Foo{
    
       //method which returns Foo
      Foo getFoo();
    
    }
    
     public class Fizz implements Foo{
    
      //Overridden method which returns Fizz(as it implements Foo)
      @Override
      Fizz getFoo(){
          //your code         
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题