Method Overloading

icon



Hello there, Welcome to programming scoop tutorial.Today we will learn what is method overloading in java and how it is useful and how it is done.

So let's start this  tutorial

What is method loading?

 
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.

When more than one method in a class having the same name but different signatures like no. of parameter,type of parameter,return type of method then the method is said to be overloaded.
 

for ex:- area();area(int a),area(int a,int b);area(float a,float b);
   


method oveloading



This all above methods have same but having different signature which perform different task.

Basically method overloading is to  use the same name for creating methods in a class but having different functionality then thid methods are called overloaded method.

Now, A question is arise how compiler knows which method is called. so Java compiler depends upon two main thing to determine which overloaded method is called.These two things are:-

  • Number of arguments.
  • Type of arguments.

Uses of method overloading:-
  • Method overloading is the way to implement static or compile time polymorphism.
  • Method overloading enable us to use the same name for different methods which makes the program more readable.

    How Method Overloading is done? 

    To understand method overloading let's take a example program.
    It is simple program to calculate area of rectangle and square.



    1. class calculate   
    2. {
    3.     public void area(int a)      //same name but one parameter
    4.     {
    5.         System.out.println("Area of Square is : "+(a*a));
    6.     }   
    7.     public void area(int l,int b)      //overloaded method
    8.     {
    9.         System.out.println("Area of Rectangle is : "+(l*b));
    10.     }       
    11. }
    12. class main
    13. {
    14.     public static void main(String args[])
    15.     {
    16.         calculate obj=new calculate();
    17.         obj.area(5);     //area of square
    18.         obj.area(3,5);      //area of rectangle
    19.     }
    20. }


    method overloading
    output

     


    Now I hope you understand the Concept of Mehtod Overloading in java. If you have any doubt or query in any program then feel to ask in comment section or any social media sites.

    Visit more post on programmingscoop to understand more concept of java