Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a method with different return types in java?

I have read a book and it says I can override a method if it has the same signature. according to the book the signature of a method is Method_Name + Parameters passed.

as per the book, i can override a method which has different return types. Is it actually possible to override a method with different return type in Java? because i have done a some search on the net i found people saying that to override a method the return type should be same as well.

according to the book it also says the java will throw a compile error when we try to overload a method with same method name and parameters but different return types since the signature means only the method name and parameters. If this is true, we should be able to override a method with different return type.

Please help me to understand this. Thanks in advance.

like image 590
nzhmz Avatar asked Jan 25 '13 10:01

nzhmz


People also ask

Can overriding methods have different return types in Java?

The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.

Can you override with a different return type?

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.

Should return type be same in method overriding?

When we override a parent class method, the name, argument types, and return type of the overriding method in child class has to be exactly the same as that of the parent class method. The overriding method was said to be invariant with respect to return type.

Is method overriding is possible by changing the return type of method?

It is not possible to decide to execute which method based on the return type, therefore, overloading is not possible just by changing the return type of the method.


5 Answers

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.

like image 61
JB Nizet Avatar answered Oct 12 '22 18:10

JB Nizet


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.

like image 31
David Lavender Avatar answered Oct 12 '22 16:10

David Lavender


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
        }
    }
}
like image 25
Evgeniy Dorofeev Avatar answered Oct 12 '22 18:10

Evgeniy Dorofeev


Here is an example:

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

class A extends Base {
    public Long test() {
        return 1L;
    }
}
like image 41
jdb Avatar answered Oct 12 '22 18:10

jdb


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.

like image 43
PermGenError Avatar answered Oct 12 '22 17:10

PermGenError