JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

What is method overloading in Java with Example


What is method overloading in Java


Method overloading in Java is a programming concept when programmer declares two methods of the same name but with different method signature, e.g. change in the argument list or change in the type of argument. method overloading is a powerful Java programming technique to declare a method which does a similar performance but with a different kind of input. One of the most popular examples of method overloading is System.out.println() method which is overloaded to accept all kinds of data types in Java. You have println() method which takes String, int, float,double or even char in output. All of those methods are collectively referred as an overloaded method in Java. The difference between method overloading and overriding is also a popular Java interview question. In next section, we will some important points about method overloading in Java and then a simple example of how to overload a method in Java.



Properties of method overloading in Java

1) Overloaded methods are bonded using static binding in Java. Which occurs during compile time i.e. when you compile Java program. During the compilation process, compiler bind method calls to the actual method.

2) Overloaded methods are fast because they are bonded during compile time and no check or binding is required during runtime.




3) Most important rule of method overloading in Java is that two overloaded methods must have a different signature.Here is an example of What does method signature means in Java:

1) A number of argument to a method is part of method signature.
2) Type of argument to a method is also part of method signature
3) Order of argument also forms part of method signature provided they are of different type.
4) The return type of method is not part of the method signature in Java.



Method Overloading Example in Java:


What is method overloading in Java with Example

Here is a list of method and there corresponding overloaded method with reason that How they are overloaded :

Original method :
 public void  show(String message){
      System.out.println(message);
}

Overloaded method: the number of argument is different
 public void  show(String message, boolean show){
      System.out.println(message);
}

Overloaded method: type of argument is different
 public void  show(Integer message){
      System.out.println(message);
}
Not a Overloaded method : only return type is different
 public boolean show(String message){
      System.out.println(message);
      return false;
}

In summary method, overloading means multiple methods with the same name but with a different signature. remember return type is not part of method signature. method overloading is also completely different to method overriding which is a similar concept and we will see in next article. That's all on What is method overloading in Java, let me know if you have any question related to How to overload a method in Java.



Method overloading in any programming language, including Java, will work fine if:

- the two variants have a different number of parameters
- the two variants have the same number of parameters but the types are unrelated

In both cases, the overloaded method should logically consist of the same name and operation, but with different kind of inputs.

For example, this is fine:

LocalTime.of(int hour, int minute)
LocalTime.of(int hour, int minute, int second)

as both the method are actually creating time but with the different set of input, so the name of() is valid and there is on confusion between these two methods.

Here is another example, which is also fine:

Bundle.of(String[] data)
Bundle.of(Iterable data)

(there is no possible confusion between the types, the overloaded method is providing flexible input handling for callers, both methods must perform the same logical task and that's why the same name)

But if you have the same number of parameters and related types, then it's a big problem. That should be avoided as it causes users great difficulty in figuring out which method is called
If two overloaded methods actually behave differently, then they also fail the rule that overloads should be logically equivalent.


.