• Register

Can I overload static methods in Java?

To Overload is a one of the tool to attain polymorphism, a class comprises two  functions with similar name and dissimilar parameters.

Every time you invoke this function the function body will be bound with the function call founded on the parameters.

Method

 

public class Calculation {

   public int sum(int abc , int def){

      int output = abc+def;

      return output;

   }

   public int sum(int abc , int def, int ghi){

      int output = abc+def+ghi;

      return output;

   }

   public static void main(String args[]){

      Calculation call1 = new Calculation();

      System.out.println(call1.sum(12, 13, 15));

   }

}

Outcome

40

Overloading static methods

 

The static word is used to generate approaches that will occur individually of any instance produced for the class. Static methods  not utilize any instance var of any obj of the class they are declared in.

 

The println()  is alike to print()  but that it transfers the cursor to the following line after printing the outcome. Used when you need the outcome in two distinct lines. It is named with "out" obj.

Yes, we can overload static methods in Java.

Method

 

public class Calculation {

   public static int sum(int abc , int def){

      int outcome = abc+def;

      return outcome;

   }

   public static int sum(int abc , int def, int ghi){

      int sum = abc+def+ghi;

      return outcome;

   }

   public static void main(String args[]){

      System.out.println(Calculation.sum(12, 13));

      System.out.println(Calculator.sum(12, 13, 15));

   }

}

Outcome

25

40

 

2,760 points
7 4