Overloading of Methods in Java

In this post, you will learn to:

  • Explain the concept of overloading.
  • Describe overloading using different number of parameters and same data types.
  • Describe overloading using same number of parameters and different data types.
  • Discuss constructor overloading.
  • State the use of this keyword.

Concept of Overloading

Method overloading is the ability of a class to define several methods with the same name. A generic example for overloading would be the print() method. The same method can be used to print a document, an image, and so forth. Instead of defining methods with different names to achieve this, it would be sensible to have one name shared across multiple methods.

Sample code below shows method overloading in a class.

print (int number) {
    ...;
}
print (String name) {
    ...;
}

Overloading: Different Parameter List

Overloaded methods may comprise different number of parameters and same data types. The following code demonstrates method overloading with different parameter list

public class PayRoll {
   int basicsalary;
   int hra;
   int tax;
   int netsalary;
 
   public void CalcSalary(int empid){
      System.out.println("Calculating salary for employee");
      netsalary= basicsalary + hra - tax;
   }
 
   public void CalcSalary(int empid, int leavetaken){
      System.out.println("Calculating salary for employee based on leave taken");
      netsalary= basicsalary + hra – tax – (leavetaken * 500) ;
   }

   public void CalcSalary(int empid, int leavetaken, int incentives){
      System.out.println("Calculating salary for employee based on leave taken and incentives");
      netsalary= basicsalary + hra - tax -(leavetaken * 500) + incentives;
   }
 
   public static void main(String[] args) {
      PayRoll objPay = new PayRoll();
      objPay.CalcSalary(10);
      objPay.CalcSalary(10,4);
      objPay.CalcSalary(10,4,5000);
   }
}

As shown in the code, the class defines a method CalcSalary() which is overloaded a number of times. The method signatures for CalcSalary() differ in number of parameters but have the same data type, int. When CalcSalary is called with one int argument, it invokes CalcSalary() that is defined with a single int parameter. When CalcSalary() is called with two int arguments, it invokes CalcSalary() that is defined with two int parameters.

Overloading: Different Data Types

Overloaded method signatures may comprise same number of parameters and different data types. The following code demonstrates overloading of methods with fixed number of arguments with different data types.

public class Printing {
 
   public void print(int fileId) {
      System.out.println("Printing an integer value");
   }
 
   public void print(String filename) {
      System.out.println("Printing a string value");
   }
 
   public void print(double sum){
      System.out.println("Printing a double value");
   }
 
   public static void main(String[] args) {
      Printing objPrint = new Printing();
      objPrint.print(101);
      objPrint.print("Welcome");
      objPrint.print(456.75);
   }
}

In the code, class Printing defines three print() methods, all having the same name, same number of parameters but of different data types. Each of these methods has a single parameter. However, these methods can be overloaded as they comprise different data types.

Constructor Overloading

Similar to methods, constructors can be overloaded. The rules for method overloading and constructor overloading are the same. Hence, constructor overloading can be either with same number of parameters and different data types or different number of parameters and same data types. A constructor having parameters is known as parameterized constructor. Thus, constructor overloading allows multiple ways of creating instances.

The following code demonstrates constructor overloading.

public class Printing {

   public Printing() {
      System.out.println("Printing a document");
   }
 
   public Printing(int fileId) {
      System.out.println("Printing a document using fileId");
   }
}

As shown in the code, the two constructors are overloaded; one with no parameters and the other with one parameter.

‘this’ Keyword

The this keyword in Java is used in a constructor or instance method to refer to the current object in memory. The keyword allows access to a member of the current object. this keyword is used to access members that are presently shadowed or hidden by a local variable with the same name.

The following code demonstrates the use of this keyword.

public class Printing {
   int fileId;
 
   public void print(int fileId){
      this.fileId= fileId; 
   }
}

As shown in the code, the class Printing declares a member fileId. Next, the print method accepts a parameter named, fileId. Now, it is required to assign to the member the value of the parameter that has been passed. However, as both of them have the same names, the member is shadowed or hidden by the parameter. Hence, to access the member, the this keyword is used.

Related Post