Java-Method Overloading

Method Overloading

Two or more methods in a class with the same name but different parameter. This technique is called method overloading. Method overloading is used when objects are required to perform similar tasks but using different input parameters. When we call a method in an object, Java matches up the method name first and then the number and type of parameters to decide which one of the definitions to execute. To create an overloaded method, all we have to do is to provide several different method definitions in the class, all with the same name, but with different parameter lists. The difference may either be in the number or type of arguments. That is, each parameter list should be unique. The return value of the method is not taken into consideration.

Program

31

Program Source 

class Data{
    
    void data()
    {
        System.out.println("Data not found");
    }
    
    void data(char c)
    {
        System.out.println("Character found");
    }
    
    void data(int i)
    {
        System.out.println("Integer found");
    }
    
    void data(String str)
    {
        System.out.println("String found");
    }
    
    void data(double dou)
    {
        System.out.println("Double found");
    }
}

public class Javaapp {

    public static void main(String[] args) {
       
        Data d1 = new Data();
        d1.data();
        d1.data('J');
        d1.data(5000);
        d1.data("Java");
        d1.data(9.0);
    }
}

Leave a Comment