The relationship of overload and method return type in Java?

前端 未结 4 1160
-上瘾入骨i
-上瘾入骨i 2020-12-06 21:04

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

int test(int p) {
   System.out.p         


        
相关标签:
4条回答
  • 2020-12-06 21:38

    Yes, this is also an overload. Since only the name and the list of parameters are considered part of method's signature for the purposes of method overloading, both your test methods are overloads of each other.

    There may be useful scenarios for overloading a method like that, too. Consider this example:

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

    A programmer who uses Sanitizer can write things like

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

    and the overload makes the code looks the same for variables of different types.

    0 讨论(0)
  • 2020-12-06 21:40

    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;
          }
      }
      
    0 讨论(0)
  • 2020-12-06 21:53

    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 to this that will be discussed in the lesson titled "Interfaces and Inheritance").

    Having a different return type is inconsequential to overloading. In fact, this is quite common with methods that return one of their arguments. E.g., java.util.Math has a bunch of overloaded max methods. A max of two ints return an int, a max of two doubles return a double, etc.

    0 讨论(0)
  • 2020-12-06 21:56

    In function overloading return types don't play any role. Function overloading can only be achieved via change in arguments. So, yes in your given case test() is overloaded

    0 讨论(0)
提交回复
热议问题