Sunday, August 26, 2012

What is method overloading in Java - Example Tutorial

What is method overloading in Java
Method overloading in Java is a programming concept when programmer declare two methods of same name but with different method signature e.g. change in argument list or change in type of argument. method overloading is a powerful Java programming technique to declare method which does similar performance but with different kind of input. One of the most popular example 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 refereed as overloaded method in Java. 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

What is method overloading in Java with Example
1) Overloaded methods are bonded using static binding in Java. Which occurs during compile time i.e. when you compile Java program. During compilation process, compiler bind method call to 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 method must have different signature.Here is an example of What does method signature means in Java:

1) 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) return type of method is not part of method signature in Java.

Method Overloading Example in Java
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 method with same name but with different signature. remember return type is not part of method signature. method overloading is also completely different to method overriding which is 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.

Related Java Tutorials
Difference between TreeSet and TreeMap in Java
What is Serialization in Java
Difference between wait and sleep in Java
Difference between ArrayList and HashMap in Java
What is class file in Java 

1 comment:

Java67 Headline Animator