Tuesday, 21 April 2015

Method overloading in Java

What is Method overloading

When two or more methods with in the same class or with in the parent-child relationship classes have the same name, but the parameters are different in types or number the methods are said to be overloaded. This process of overloaded methods is known as method overloading.
Method overloading is one of the ways through which java supports polymorphism.

When an overloaded method is called, which method is to be called is determined through -

  • Types of the parameters.
  • Number of the parameters.

Please note that return type of the methods may be different but that alone is not sufficient to determine the method that has to be called.

Let's see an example of method overloading -

public class OverloadingExample {
 // Method with 1 int param
 void overloadedMethod(int i){
  System.out.println("In overloadedMethod with one int parameter " + i);
 }
 
 // Method with 2 int params
 void overloadedMethod(int i, int j){
  System.out.println("In overloadedMethod with 2 int parameters " + i + " " + j);
 }
 
 // Method with 1 double param
 void overloadedMethod(double i){
  System.out.println("In overloadedMethod with 1 double parameter " + i);
 }
 
 public static void main(String args[]){ 
  OverloadingExample obj = new OverloadingExample();
  obj.overloadedMethod(5);
  obj.overloadedMethod(5.7);
  obj.overloadedMethod(5, 10);
 }
}

Here we have 3 methods with the same name but parameters are different either in number or type. One method has 1 int param, another method has 2 int params and yet another has 1 double param.

Output of the program

In overloadedMethod with one int parameter 5
In overloadedMethod with 1 double parameter 5.7
In overloadedMethod with 2 int parameters 5 10

Method overloading and automatic type conversion

In java automatic type promotion may happen and it does have a role in how overloaded methods are called, let's see it with an example to have clarity.

public class OverloadingExample {
 // overloaded Method with 1 int param
 void overloadedMethod(int i){
  System.out.println("In overloadedMethod with one int parameter " + i);
 }
 
 // overloaded Method with 2 double params
 void overloadedMethod(double i, double j){
  System.out.println("In overloadedMethod with 2 double parameters " + i + " " + j);
 }
 
 
 public static void main(String args[]){ 
  OverloadingExample obj = new OverloadingExample();
  obj.overloadedMethod(5);
  obj.overloadedMethod(5.7, 103.78);
  obj.overloadedMethod(5, 10);
 }
}

Here notice the third call to the method
obj.overloadedMethod(5, 10);
It has 2 int params, but there is no overloaded method with 2 int params, in this case automatic type conversion happens and java promotes these 2 int parameters to double and calls overloadedMethod(double i, double j) instead.

Overloaded method in case of Inheritance

In the case of parent-child relationship i.e. inheritance if two classes have the method with same name and signature then only it is considered as method overriding, otherwise it is method overloading only.

class Parent {
 private int i;
 // Constructor
 Parent(int i){
  this.i = i;
 }
 // Method with no param
 public void dispayData(){
  System.out.println("Value of i " + i);
 } 
}

class Child extends Parent{
 private int j;
 // Constructor
 Child(int i, int j){
  // Calling parent class constructor
  super(i);
  this.j = j;
 }
 // Overloaded Method with String param
 public void dispayData(String showMsg){
  System.out.println(showMsg + "Value of j " + j);
 } 
}

class OverloadDemo{
 public static void main(String args[]){ 
  Child childObj = new Child(5, 10);
  // This call will invoke the child class method
  childObj.dispayData("in Child class displayData ");
  // This call will invoke the parent class method
  childObj.dispayData();
 }
}

Output of the program would be

in Child class displayData Value of j 10
Value of i 5

Benefit of Method Overloading

If we don't have method overloading then the methods which have same functionality but parameters type or number differs have to have different names, thus reducing readability of the program. As exp if we have a method to add two numbers but types may differ with out overloaded methods then there will be different methods for different types like addInt(int a, int b), addLong(long a, long b) and so on. With method overloading we can have the same name for the overloaded methods thus increasing the readability of the program.

That's all for this topic Method overloading in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Polymorphism in Java
  2. Method overriding in Java
  3. Java OOP interview questions
  4. Inheritance in Java

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment