Overloading

Back to home
Logicmojo - Updated Jan 11, 2022



What is Overloading?

Two methods are said to be overload if and only if both having the same name but different argument types.Overloading refers to the ability to define multiple methods of a class with different input and output parameters using a single identifier. When two or more methods execute the same task but with slightly different parameters, they are said to be overloaded.

Overloading is a technique for avoiding repetitive code in which the same method name is used many times but with different parameters each time. Runtime errors are avoided because the actual method that is called at runtime is resolved at compile time. Overloading simplifies code, reduces complexity, and improves runtime performance.

In overloading compiler is responsible to perform method resolution(decision) based on the reference type. Hence overloading is also considered as compile time polymorphism(or) static ()early biding.

Method overloading can be achieved by the following:

  1. By changing the number of parameters in a method

  2. By changing the order of parameters in a method

  3. By using different data types for parameters

Example

class Test
{
	public void methodOne()
	{
		System.out.println("no-arg method");
	}
	public void methodOne(int i)
	{
		System.out.println("int-arg method");	     
	}
	public void methodOne(double d)
	{
		System.out.println("double-arg method");
	}
	public static void main(String[] args)
	{
		Test t=new Test();
		t.methodOne();//no-arg method
		t.methodOne(10);//int-arg method
		t.methodOne(10.5);//double-arg method
	}
}

Automatic Promotion in Overloading

In overloading if compiler is unable to find the method with exact match we won’t get any compile time error immediately.

First compiler promotes the argument to the next level and checks whether the matched method is available or not if it is available then that method will be considered if it is not available then compiler promotes the argument once again to the next level. This process will be continued until all possible promotions still if the matched method is not available then we will get compile time error. This process is called automatic promotion in overloading.

The following are various possible automatic promotions in overloading.

Example

class Test
{
	public void methodOne(int i)
	{
		System.out.println("int-arg method");
	}
	public void methodOne(float f)			
	{
		System.out.println("float-arg method");
	}
	public static void main(String[] args)
	{
		Test t=new Test();
		t.methodOne('a');//int-arg method
		t.methodOne(10l);//float-arg method
		//t.methodOne(10.5);//C.E:cannot find symbol
	}
}

Advantages of Overloading

  1. The program is very simple and also easier to understand.

  2. Function overloading is used for code reusability and to save memory.

  3. Using the function overloading concept, we can develop more than one function with the same name, but the arguments passed should be of different types.

  4. It saves the memory space, maintains consistency, makes clear interface for methods regardless of the parameter’s type and readability of the program.

  5. Function overloading executes the program faster.

Disadvantages of Overloading

  1. Function declarations that differ by their return type cannot be overloaded with the function overloading process.

  2. If any static member function is declared, then the same parameters or the same name types cannot be overloaded.



With this article at Logicmojo, you must have the complete idea of overloading in OOPs.