Overriding a method with different return types in java?

心已入冬 提交于 2019-11-27 13:14:27

You can return a different type, as long as it's compatible with the return type of the overridden method. Compatible means: it's a subclass, sub-interface, or implementation of the class or interface returned by the overridden method.

And that's logical. If a method returns an Animal, and your derived class returns a Cow, you're not breaking the contract of the superclass method, since a Cow is an Animal. If the derived class returns a Banana, that isn't correct anymore, since a Banana is not an Animal.

Your parent class has made a promise to the outside world. For example, the method:

public Price calculatePrice(Items[] items).

It tells the world to expect a Price.

If you enhance that functionality in your subclass, you still have to keep your parent classes' original promises for it.

You can add overloaded ways of calculating:

public Price calculatePrice(Items[] items, Integer minimumCharge).

You can even improve your parent's promises by using a MORE specific return type:

public AccuratePrice calculatePrice(Items[] items, Integer minimumCharge).

But you must return at least the type that your parent promised. The same goes for Exceptions in the method declaration too.

Yes, it is possible since Java 5, it is called covariant return type. The return type should be a subcass of super class method return type (primitive types are not allowed). Example

class X implements Cloneable {

    @Override
    protected X clone() {
        try {
            return (X) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new Error(e); // can never happen
        }
    }
}

Here is an example:

class Base {
    public Number test() {
        return 0;
    }
}

class A extends Base {
    public Long test() {
        return 1L;
    }
}

Your overriden method can have same type or the sub-type of the original return type which is called as covariant return.

If you change the return type of the overriden method to something else which is not a sub-type of the original type, then you'd get a compile time error.

Yes we can override different return types but they should be subclass.

public class Shape {
    public Shape area(Integer i) {
        System.out.println("Sape Area");
        System.out.println("Integer");
        return null;
    }
}


package com.oops;

public class Circle extends Shape {
    public Circle area(Integer i) {
        System.out.println("Circle Area");
        System.out.println("int");
        return null;
    }
}
// Covariant Overriding 
public class Parent {

  public Parent(){}
  String parentName;
  public Parent(String parentName){
    this.parentName=parentName;
    System.out.println(this.parentName);
 }

public  Parent show(){
    return new Parent("Parent");
}
}




public class Child extends Parent{

  public Child(){}
  String name;
  public Child(String name){
    this.name=name;
    System.out.println(this.name);
  }
  public Child show(){
    return new Child("Child");
}
}



public class Main {

public static void main(String[] args) {
    Parent parent=new Child();
    parent.show();

    Parent parent1=new Parent();
    parent1.show();
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!