What Is Method Overloading?

Method overloading refers to the definition of multiple methods with the same name in a class, but each method requires different types of parameters or the number of parameters. When calling an overloaded method, the Java compiler can select an appropriate method by checking the type and number of parameters of the called method. Method overloading is usually used to create a method that accomplishes a similar set of tasks but has a different parameter type or number of parameters or a different order of parameters. [1] Java method overloading, that is, you can create multiple methods in the class, they can have the same name, but must have different parameters, that is, either the number of parameters is different, or the parameter types are different. When calling methods, decide which method to use by passing different numbers and types of parameters to them, and the order of the parameters passed in

Method overloading

Method overloading refers to the definition of multiple methods with the same name in a class, but each method requires different types of parameters or the number of parameters. When calling an overloaded method, the Java compiler can select an appropriate method by checking the type and number of parameters of the called method. Method overloading is usually used to create a method that accomplishes a similar set of tasks but has a different parameter type or number of parameters or a different order of parameters. [1]
Method overloading:
Method overloading means duplicate method names and different loading parameters.
  class MethodOverloading {

 void receive (int i) {
 System.out.println ("Received one int data");
 System.out.println ("i =" + i);
 }

 void receive (float f) {
 System.out.println ("Received one float data");
 System.out.println ("f =" + f);
 }

 void receive (String s) {
 System.out.println ("Received a String");
 System.out.println ("s =" + s);
 }

 public static void main (String [] args) {
 MethodOverloading m = new MethodOverloading ();
 m.receive (3456);
 m.receive (34.56f);
 m.receive ("Method Overload");
 }
 }

Note that method overloading in Java requires methods with the same name to have different parameter tables. The difference in return type is not enough to distinguish between two overloaded methods.
First, the method name must be the same.
2. The parameter table of the method must be different, including the type or number of parameters, so as to distinguish different method bodies.
1. If the number of parameters is different, it does not matter its parameter type!
2. If the number of parameters is the same, the types of the parameters must be different.
The return type and modifier of the method can be the same or different.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?