Overloading in Java

Overloading:

Methods are said to be overloaded if they have same method names but different arguments.

Consider the below example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Sample {
  public static void m1() {
    System.out.println("No arg!");
}
 
public static void m1(int i) {
    System.out.println("Int arg");
}
public static void m1(float f) {
    System.out.println("Float arg");
}
 
 public static void main(String [] args) {
    Sample s1 = new Sample();
s1.m1(); //No Arg!
s1.m1(10); // Int-arg
s1.m1(10.5f); //Float arg
} 
}

In the above example m1() is overloaded, we can observe that even when the method is same, the output is different when we pass different arguments.Overloading concept in java reduces the complexity of programming.

More factors to observe in overloading :

1. Automatic promotion :

When resolving overloaded methods if exact method is not available the we do not get any compile time error immediately, the compiler first promotes the arguments to the next level and checks whether the matched method is available, if its available then compiler considers it, otherwise promotes it to next level. This process will continue untill all possible promotions are completed. If no match is found we would get compiler error!

To illustrate this lets modify the above example a bit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Sample {
 
public static void m1(int i) {
    System.out.println("Int arg");
}
public static void m1(float f) {
    System.out.println("Float arg");
}
 
 public static void main(String [] args) {
    Sample s1 = new Sample();
s1.m1(10); // Int-arg
s1.m1(10L); //Float arg
s1.m1(10.5f); //Float arg
s1.m1(10.5); // compiler error "Cannot find symbol"
} 
}

In the above example you can notice a long is automatically promoted to Float and hence we get output as “Float arg” , but a double cannot be downgraded to float hence compiler error.

2. In overloading, child class will get more priority than object class.

Lets take a couple of examples to illustrate this point more clearly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Test 
{
	public void m1(Object o) 
	{
		System.out.println("Object here");
	}
 
	public void m1(String s) 
	{
		System.out.println("String here");
	}
 
	public static void main(String[] args) 
	{
		Test t = new Test();
		t.m1("Harsha"); //String here
		t.m1(new Object()); //Object here
		t.m1(null); //String here
	}
}

As you can see in above example, when we pass null in m1() String version method got executed, since the child class methods gets more priority.

Nows lets take another example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Test 
{
	public void m1(StringBuffer sb) 
	{
		System.out.println("String Buffer");
	}
 
	public void m1(String s) 
	{
		System.out.println("String here");
	}
 
	public static void main(String[] args) 
	{
		Test t = new Test();
		t.m1("Harsha"); // String version
		t.m1(new StringBuffer("Harsha")); //StringBuffer Version
		t.m1(null); // Compiler Error - reference to null is ambigious
	}
}

As we can notice, in the above example we will get compile time error when we try to pass null since both String and StringBuffer are subclasses of object.

3. In overloading method resolution is always taken care by compiler based on reference type. Runtime object wont play any role.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Animal
{
}
class Monkey extends Animal
{
}
class Test 
{
   public void m1(Animal a) 
	{
		System.out.println("Animal version");
	}
 
	public void m1(Monkey m) 
	{
		System.out.println("Monkey version");
	}
 
	public static void main(String[] args) 
	{
		Test t = new Test();
		Animal a = new Animal();
                 t.m1(a); //Animal Version
		Monkey m = new Monkey();
		t.m1(m); //Monkey version
		 a = new Monkey();
		t.m1(a); // Animal version
	}
}

In last method even when we pass a Monkey object the output is “Animal version” since the reference type is of Animal and compiler only cares about reference type.

 

Thats all about overloading. Please comment if you have any doubt.

Next lesson “Overriding in java”

2 Responses

  1. Day1 – OOPS
    Day1 – OOPS March 19, 2013 at 5:06 am |

    […] Overloading , Overriding […]

  2. Anurag Singh
    Anurag Singh June 29, 2017 at 9:23 am | | Reply

    this is nice post about method overloading in java.
    it is very useful for us.
    thanks for this nice post.

Leave a Reply