Overloading Method

In this section, we will discuss about the Overloading method in Java. Overloading is the core concept of java which makes the code very simple, clean, crispy & easy to manage.

Objectives:

  • Overloading method
  • Why Overloading is needed
  • How to achieve the Overloading?
  • Overloading Constructor
  • Examples

Overloading Method:

When two or more methods are defined inside the same class with the same method name but with different arguments then this process is called Method Overloading in Java. In OOPs, this is also called as Static binding or Static Polymorphism. (We will discuss on Polymorphism later). JVM invokes the method according to type and number of argument mentioned in methods signature when this overloaded method is invoked. As we know Method signature consist of “Method Name”, ”Type of the Argument”, Number of the argument.

Syntax:

void show(){
----------
----------
----------
}
int show(int age){
----------
----------
----------
}
double show(double salary) {
----------
----------
----------
}

Why we need Overloading?

Method overloading allows you to define more  than one method with same method name. This will help programmer to manage the methods with functionality easily. Let’s say you need to execute only operation and handle multiple set of inputs then defining different method for all set will create confusion for method also hard to manage in application code for bigger application. But if you use same method name to execute the same functionality by providing different set of argument then it will increase the readability of the application and easy to manage the code also.

Also we do not need to remember different function name for performing same action.

For example: if you want to calculate the area of different geometrical diagram like Circle, Square, Rectangle etc. then you can create the a single method with name “area()” and passing argument with respect to Circle, Square, Rectangle etc. So that we can call the area method by passing the argument separately for Circle, Square, Rectangle etc.

In this way our application code will be clean and crispy and easy to manage.

How to achieve the Overloading?

Overloading is very important concept in Java. This makes out code very simple and crispy and easy to manage. But how to achieve this Overloading?

There are 2 ways to achieve Overloading in java.

  • Type of Argument
  • Number of Argument
Overloading Method

From the name we can guess the purpose of these 2 ways. In first way, we can pass different type of argument to the method signature. In second way we can achieve overloading by changing the number of argument in method signature.

Let’s understand this with a simple example:

Example:

class OverloadMethod
{
    void area(float side)
    {
        System.out.println("Area of the Square is "+Math.pow(side, 2)+" sq units");
    }
    void area(float height, float width)
    {
        System.out.println("Area of the Rectangle is "+height*width+" sq units");
    }
    void area(double radius)
    {
        double a = 3.14 * radius * radius;
        System.out.println("Area of circle is "+a+" sq units");
    }
}
class OverloadMethodDemo
{
     public static void main(String args[]) 
	{
    	 OverloadMethod om = new OverloadMethod();
	   om.area(5);
	   om.area(11,12);
	   om.area(2.5);
        }
}

Output:

Area of the Square is 25.0 sq units
Area of the Rectangle is 132.0 sq units
Area of circle is 19.625 sq units

Explanation:

From the above example you can notice that, we have declared only one method area() and overloading this area method with different argument. To find area of Circle, we need only one double type variable argument. So, we define the area method with one argument of double type. Like this we need only one float variable argument to calculate the area of square i.e. side. But we need 2 variable argument to calculate the area of Rectangle i.e. height & width. For this we have overloaded the area method with 2 float variable argument.

When we are calling the area method, we just need to paly with argument to find the area of Circle, Rectangle, Square.

In first method call we have given only one argument of float type. So It will point to the area of square, same as in second method call, we have given 2 argument of float type. It is pointing to the area of Rectangle. Like this, in 3rd method call, this is pointing tot the area of Circle method.

Overloading Constructor:

Like Method overloading, Constructor can be overloaded in java. This is the feature in java in which you can overload the more than one constructor in a class. This is managed in a such way that each constructor can perform different task. Compiler differentiates the number of argument / parameter and type of the parameter.

Why we need Constructor Overloading?

Sometimes it is required to initialize the object in a different way. If we want to initialize default object then we will declare the default constructor. We can pass different parameters in constructor if we want to customize the initialization in object.

Let’s understand the Constructor Overloading with a simple example.

Example:

class Employee{  
    int id;  
    String name;  
    int age;  

    Employee (int i, String n){  
    id = i;  
    name = n;  
    }  

    Employee (int i, String n, int a){  
    id = i;  
    name = n;  
    age=a;  
    }  
    void details(){System.out.println(id+" "+name+" "+age);}  
   
    public static void main(String args[]){  
    	Employee s1 = new Employee (876453,"Pratik");  
    	Employee s2 = new Employee (553879,"Ram",25);  
    s1.details ();  
    s2.details ();  
   }  
}  

Output:

876453 Pratik 0
553879 Ram 25

Explanation:

Form the above example, we can notice that we have declared 2 constructor by passing different number of the parameters/ argument.

Note: if you have defined the parameterized constructor, then compiler will not create any default constructor bydefault. Also if you have not defined any parameterized constructor, only defined default constructor then also compiler will not create any parameterized constructor bydefault.
Note: Recursive constructor not possible in Java.

Summary:

  • Overloading is the core concept of java which makes the code very simple, clean, crispy & easy to manage.
  • When two or more methods are defined inside the same class with the same class name but with different arguments then this process is called Method Overloading in Java. In OOPs, this is also called as Static binding or Static Polymorphism.
  • Method overloading allows you to define more  than one method with same method name. This will help programmer to manage the methods with functionality easily.
  • Like Method overloading, Constructor can be overloaded in java. This is the feature in java in which you can overload the more than one constructor in a class.
  • Sometimes it is required to initialize the object in a different way. If we want to initialize default object then we will declare the default constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *