Tutorial: Method Overloading in Java with Code

Since now you are familiar with instantiating inner classes in Java, we can talk about Polymorphism and Method Overloading. Polymorphism is the capability of a Java object to respond uniformly to achieve specific behavior to the method calls of the same name but with different implementations. In Java, the concept of Polymorphism can be achieved in several ways. Here is our Java Tutorial with Code for Method Overloading.

Java Tutorial with Code for Method Overloading.

What is Method Overloading

In Java, it is possible to define two or more methods within the same class that share the same name. However, their parameter declarations should be different. These types of methods are said to be overloaded, and the process is referred to as method overloading.

Method overloading supports polymorphism because it is one way that Java implements the “one interface, multiple methods” paradigm.

Properties

  • In a class, the concept of method overloading does not allow the external user to be aware of the internal processing of the system.
  • Overloaded methods differ in either the number of parameters or type of parameters or sequence of parameters.
  • Overloaded methods are resolved in the following sequences:
    1. Number of Parameters.
    2. Type of Parameters.
    3. A sequence of Parameters.

On the basis of these methods are invoked accordingly to return the desired output.

  • Method Overloading allows the user to achieve the compile time polymorphism.
  • Overloaded methods may have the same or different return types.

Java Program for Method Overloading

class Figure{
  /* Calculate Area of Square */
  void area(double length){
    System.out.println("Area of Square is: "+Math.pow(length,2));
  }
  /* Calculate Area of Rectangle */
  void area(double length,double breadth){
    System.out.println("Area of Rectangle is: "+(length*breadth));
  }
  /* Calculate Area of Cylinder */
  void area(double radius,int height){
    System.out.println("Area of Cylinder is: "+(2*3.14*Math.pow(radius,2)*height));
  }
  /* Calculate Area of Cube */
  void area(int arms){
    System.out.println("Area of Cube is: "+(6*arms*arms));
  }
}

class CalculateArea{
  public static void main(String args[]){
    Figure fig = new Figure();
	fig.area(5.25);
	fig.area(4.5,5.5);
    fig.area(2);
	fig.area(3.2,6);
  }
}

The output of Method Overloading.

Java Tutorial with Code for Method Overloading.

Explanation of the Java Code & Output

Here within class Figure area() is an overloaded method, having the same name with different implementations i.e. “one interface, multiple methods” paradigm. You can easily make out that the same method is used to perform different operations i.e. calculating areas of different geometric areas based on the number/ type/ sequence of parameters passed and desired output is produced.

Method invocation may be of any order. As method area() to calculate the area of cube invoked before invoking method area() to calculate the area of the cylinder.

In the program, we have also called one static method pow() of Math class which is a member of the java.lang package to calculate the square of any number.

Flash on

During resolving overloaded methods Java employs its automatic type conversions only if no exact match is found. As you can see below that class Overload does not define display (int) but it already has defined display (double).

Therefore, when display() is called with an integer argument inside OverloadDemo, no matching method is found. However, Java can automatically convert an integer into a double, and this conversion can be used to resolve the call. Therefore, after display (int) is not found, Java elevates “i” to double and then calls display (double). Of course, if “display (int)” had been defined, it would have been called instead.

class Overload {
  // display method with no parameters
  void display() {
    System.out.println("No parameters");
  }
  // Overload display for two integer parameters.
  void display(int a, int b) {
    System.out.println("a and b: " + a + " " + b);
  }
  // overload display for a double parameter
  void display(double a) {
    System.out.println("a: " + a);
  }
}
class OverloadDemo {
  public static void main(String args[]) {
    Overload overload = new Overload();
     int i = 88;
     ob.display();
     ob.display (10, 20);
     ob.display (i); // this will invoke display(double)
     ob.display (123.2); // this will invoke display(double)
  }
}

N.B: This conversion is true for lower datatype ( e.g int) to higher datatype (e.g double) conversion not for the reverse(double to int).

In our next tutorial, we will learn about Constructor overloading in Java.

About the author

Avatar photo

William Johnson

1 Comment

Click here to post a comment