OverLoading Tutorial

You might face questions about Overloading  in most of the Java interviews. Overloading is nothing but implementing  ‘n’ of methods with the same name in a Java class, but they must have different method signature. What is method signature in Java ?

In java, method signature is as follows.

Note: Return type doesn’t include in method signature.

Overloaded methods are differentiated by the number of the arguments and the type of the arguments passed to the method. We will see different scenarios how Overloading works .

Scenario 1

OUT PUT : 

Scenario 2 

In this example, will see the return type differentiation of overloaded method.

In above example method 3 and method 4 having same signature and varying with the return type. As we know that return type is not part of the method signature. So compiler throwing an error like below.

OUT PUT :

Scenario 3

In overloading most specific method will be called, have a look in below example.

 

In above example we have two methods are overloaded with argument type (one with String and another with Object). In overloading most specific method version will be called by the compiler when you pass null value to the method.

OUT PUT

Scenario 4

What happens if we have one method with String and  another method with StringBuffer, let us have a look in below code snippet.

Here when you pass “hello” , compiler called String version , since  string is most specific than String Buffer. See the output.

OUT PUT

 

in above example , just uncomment obj.method(null) ; and see the out put.

OUT PUT

Here compiler throws error when you pass null, because compiler not able to find which is the most specific method here, because both methods are applicable but neither String nor Stringbuffer is more specific than the other. Therefore compile time error occurred.

Scenario 5

OUT  PUT

 

Scenario 6

OUT PUT

 

 Scenario 7

This scenario explains how var- args concept regarding overloading.

  OUT PUT

 

Few points on Var-args methods:

1)  you can add normal parameter with var-arg method but var-arg parameter should be last one.

example : public void method1(int a, int… b); // Valid

public void method1(int… a, int b) ; //Not valid. The variable argument type int of the method method1 must be the last parameter.

2) In any of the var-arg method, only one var-arg parameter is allowed.

public void method(int… a, int… b) //In valid

3) class Test{

public  static void method1(int[] i){

}

public  static void method1(int… i){

}

}

We cannot declare var-arg and array argument in same class. It’s a duplication.

4) We can declare single dimension array method as var-arg method.

ex:  public static void main(String[] args) —> public static void main(String… args)

public static void m1(int[] a) —> public static void m1(int… a)

5) We cannot declare var-arg parameter method as single dimensional array.

ex : public static void m1(int… a) —> public static void m1(int[] a) // NOT VALID

 

Scenario 8

Overloading with var-args method.  var-arg method will get least priority. If no other method matched then only var-arg method will get chance. Check the below example

OUT PUT

 

 

Posted in java and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">