Advertisement



< Prev
Next >



Method Overloading in Java





Method Overloading is creating a method with the same name as an existing method in a class. Hence in simple words, method overloading allows us to have multiple versions of a method within a class.




Why method overloading is used?


Method overloading is mainly used in a situation when we want to create multiple specialized versions of a method in a class, with each version having the same name and doing some specific specialized task.




How method overloading is achieved?







    Rules to method overloading -



  1. Overloaded methods MUST have different arguments.

  2. This is the most important rule to perform method overloading. In order to create multiple versions of a method is that each version of this method MUST have a different type of arguments.

    //Overloading add method of Math class.
    
    class Math
    {
    public void add(int a, int b)		//add method adding two integers
    {
    System.out.println(a+b);
    }
    
    
    public void add(float a, float b)	//add method adding two floats
    {
    System.out.println(a+b);
    }
    }
    
    class Overload
    {
    public static void main(String... ar)
    {
    Math ob = new Math();
    ob.add(1,2);
    ob.add(2.5f,4.5f);
    }
    }
    
    

    Output -

    3
    7.0
    • We have overloaded a method add() and have created its two different versions within its Math class.
    • One version of the add() method is adding two integers.
    • While the second version of the add() method is adding two floating point numbers.




  3. Overloaded methods may/may not have different return types.

  4. In the previous coding example, you have seen overloaded add() methods having a similar, void return type. In this example, we will overload the add() method with different return types.

    //Math class with overloaded add methods with different return types
    
    class Math
    {
    
    public int add(int a, int b)
    {
    return(a+b);
    }
    
    
    public float add(float a, float b)
    {
    return(a+b);
    }
    
    }
    
    class Overload
    {
    public static void main(String... ar)
    {
    Math ob = new Math();
    System.out.println(ob.add(1,2));
    System.out.println(ob.add(2.5f,4.5f));
    }
    }
    


    Output -


    3
    7.0





  5. Overloaded methods may/may not have different access modifiers.
  6. In the previous coding example, you have seen overloaded add() methods having a similar, public access modifier. In this example, we will overload eat() method of Rabbit class with a different access modifier.
    
    class Rabbit
    {
    protected void eat() 	     //eat method with protected access modifier
    {
    System.out.println("Rabbit eats carrots");
    }
    
    public void eat(String st)   //eat method with public access modifier
    {
    System.out.println("Rabbit eats "+ st);
    }
    }
    
    class Overload
    {
    public static void main(String... ar)
    {
    Rabbit ob= new Rabbit();
    ob.eat();
    ob.eat("grass");
    }
    }
    


    Output -


    
    Rabbit eats carrots
    Rabbit eats grass




  7. Overloaded methods may/may not declare new or broader checked/unchecked exceptions.
  8. Different versions of a method achieved by method overloading may/may not throw some new or broader checked/unchecked Exceptions and it's not an issue.

    class A
    {
    public void greet() throws Exception
    {
    System.out.println("Hello!");
    }
    
    public void greet(String st)
    {
    System.out.println("Hello from " + st);
    }
    }
    

    • In this code, class A has two versions of method greet(), hence it is overloaded.
    • One version of the method throws a checked exception, Exception.
    • While the other version of the method doesn't throw any exception at all.



Advertisement




Overloading through inheritance


In all the previous coding examples we saw how to create multiple versions of a method within the same class but now we will see how to create multiple versions of a method which is inherited by a class(subclass) from another class(superclass).

class Sports			//superclass
{
public void play()		//original play() method with no arguments
{
System.out.println("Play any sport");
}
}

class Tennis extends Sports	//subclass
{
public void play(String name)	//overloaded play() method with String arguments
{
System.out.println("Play "+ name);
}
}

class Overload 
{
public static void main(String... ar)
{
Tennis ob= new Tennis ();
ob.play();
ob.play("Tennis");
}
}



Output -


Play any sport
Play Tennis


Program Analysis






Please share this article -




< Prev
Next >
< Polymorphism
Overriding >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement