Overload with Different Return Type in Java

Overload with different return type in Java?

You can't do it in Java, and you can't do it in C++. The rationale is that the return value alone is not sufficient for the compiler to figure out which function to call:

public int foo() {...}
public float foo() {..}

...
foo(); // which one?

The relationship of overload and method return type in Java?

consider following points for overloading:

  1. First and important rule to overload a method in java is to change method signature. Method signature is made of number of arguments, type of arguments and order of arguments if they are of different types.

    public class DemoClass {
    // Overloaded method
    public Integer sum(Integer a, Integer b) {
    return a + b;
    }

    // Overloading method
    public Integer sum(Float a, Integer b) { //Valid
    return null;
    }
    }
  2. Return type of method is never part of method signature, so only changing the return type of method does not amount to method overloading.

    public class DemoClass {
    // Overloaded method
    public Integer sum(Integer a, Integer b) {
    return a + b;
    }

    // Overloading method
    public Float sum(Integer a, Integer b) { //Not valid; Compile time error
    return null;
    }
    }
  3. Thrown exceptions from methods are also not considered when overloading a method. So your overloaded method throws the same exception, a different exception or it simply does no throw any exception; no effect at all on method loading.

    public class DemoClass {
    // Overloaded method
    public Integer sum(Integer a, Integer b) throws NullPointerException{
    return a + b;
    }

    // Overloading method
    public Integer sum(Integer a, Integer b) throws Exception{ //Not valid; Compile time error
    return null;
    }
    }

Overriding a method with different return types in java?

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.

Can a method be overloaded with another which returns a subclass?

This example:

public Object foo() {...}
public String foo(int a) {...}

as long as the two methods get different set of variables, there's no problem with returning different types (even if they are not subclass).

The logic is very simple - if the compiler can choose without doubt which one to use - there's no issue. In this case- if you give the method int it's one method, and without parameters it's the other, no dilema (and the same name does not matter here)

as for:

public Object foo() {...}
public String foo() {...}

This one is not valid, since here the compiler can't 'choose' which one to use.

We can override method by changing return type (Covariant Return Type) in Java. Why?

Answer might sound naive but I think doing so would confuse the compiler that which function are you trying to use, if you are not using the return value of function call.

int foo(int a, int b) {...}

double foo(int a, int b) {...}

// calling one of the function and not using return value
foo(3,4);

Compiler when generates class files, it has complete method signature (including return type & package name as well) in compiled files to uniquely identify the exact function. If overloading is allowed with different return types the compiler will get confused to decide which function is being used, provided you are not using the return value.



Related Topics



Leave a reply



Submit