Relationship between overload type and return method in Java?

If there are two methods, they have different parameters and their return types are different . Like this:

int test(int p) {
   System.out.println("version one");
   return p;
}

boolean test(boolean p, int q) {
   System.out.println("version two");
   return p;
}

      

If the return types are the same, this is of course an overload . But since the return types are different, can we still consider this an overload ?

+3


source to share


4 answers


consider the following points for overloading:

1) The first and important rule of method overloading in java is changing the method signature. A method signature consists of the number of arguments, the types of the arguments, and the order of the 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) The return type of a method is never part of a method signature, so just changing the return type of a method does not mean overloading the method.



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 counted in method overloading. This way, your overloaded method throws the same exception, another exception, or just doesn't throw any exceptions; no effect at all when loading the method.

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;
    }
}

      

+1


source


Yes, this is also an overload. Since only the name and parameter list are considered part of the method signature for the purpose of method overloading, both methods test

are overloads of each other.

There may be useful scripts to overload such a method. Consider this example:

class Sanitizer {
    public static String sanitize(String s) {
        ...
    }
    public static int sanitize(int s) {
        ...
    }
    public static double sanitize(double s) {
        ...
    }
}

      



The programmer who is using Sanitizer

can write things like

String s2 = Sanitizer.sanitize(s1);
int num2 = Sanitizer.sanitize(num1);

      

and overloading makes the code the same for variables of different types.

+4


source


To quote the official tutorial :

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications for this, which will be discussed in a lesson called Interfaces and Inheritance).

Any other return type is not essential for transhipment. This is actually quite common with methods that return one of their arguments. For example, it java.util.Math

has a bunch of overloaded methods max

. A max

of two int

returns a int

, a max

of two double

returns a, double

and so on.

+4


source


In function, overloading of return types does not play any role. Function overloading can only be achieved by changing the arguments. So yes in your case test () is overloaded

+1


source







All Articles