Method Overloading in Java

Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile-time or static polymorphism. These are some example given below which will help to understand method overloading and ambiguity as well.

1) Method Overloading: changing no. of arguments
In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.In this example, we are creating static methods so that we don't need to create instance 
for calling methods
class Adder{  
static int add(int a,int b){return a+b;}  
static int add(int a,int b,int c){return a+b+c;}  
  class TestOverloading1{  
public static void main(String[] args){  
System.out.println( “Int add:”  + Adder.add(11,11));  
System.out.println(“ Double add:” Adder.add(11,11,11));  

Program output

       Int  add: 22
       Double add:33

2) Method Overloading: changing data type of arguments
In this example, we have created two methods that differs in data The first add method receives two integer arguments and second add method receives two double arguments.

class   Adder{ 
static int add(int a, int b){return a+b;} 
static double add(double a, double b){return a+b;} 
class  OveloadMethod2{ 
public static void main(String[] args){ 
System.out.println( "int add::"    +Adder.add(11,11)); 
System.out.println( " double add::"   +Adder.add(12.3,12.6)); 


Program output :

int add::22
double add::24.9

3) Why Method Overloading is not possible by changing the return type of method only?
In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:
class Adder{  
static int add(int a,int b){return a+b;}  
static double add(int a,int b){return a+b;}  
class TestOverloading3{  
public static void main(String[] args){  

In this  program  add() is not overloaded  but  it  is example of method ambiguity. This is because  jvm  will get confuse  , which method  suppose to call.

4)Static method overload can be overload

 public class StaticMainJava {

    // Normal main()

    public static void main(String[] args)


        System.out.println("Hi Lashhmi (from main)");



    // Overloaded main methods

    public static void main(String arg1)


        System.out.println("Hi, " + arg1);

        StaticMainJava.main("Mukesh ", "Welcome");


    public static void main(String arg1, String arg2)


        System.out.println("Hi, " + arg1 + ", " + arg2);

Program  output :--

Hi Lashhmi (from main)
Hi, Welcome
Hi, Mukesh , Welcome

Process finished with exit code 0

Example   of  overload  and  priority

public class Main {
    public static int foo(int a) { return 10; }
    public static char foo(int a, int b) { return 'a'; }
    public static void main(String args[])
        System.out.println(foo(1, 2));

Output ::

 10 ,a

Method Overloading in Java Method   Overloading  in Java Reviewed by Mukesh Jha on 12:02 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.