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.

Explain about overloading in Java with Example l

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 the 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 : 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.

.