Method Overloading

 

Method Overloading

Method Overloading is when we create multiple methods with the same name but pass different types of parameters inside it. This allows us to load the same methods many times. We only either need to pass a different type or a different number of parameters inside it.

 

Example:


public class MethodOverloadEx {
    static void Details(String name, int marks) {
        System.out.println("Welcome " + name);
        System.out.println("Your got "+ marks + " marks in exam.");
    }
    
    static void Details(String name, double marks) {
        System.out.println("Welcome " + name);
        System.out.println("Your got "+ marks + " marks in exam.");
    }
    
    public static void main(String[] args) {
        Details("Kapil", 89);
        Details("Maxon", 93.5);
    }
}

 

Output:


Welcome Kapil
Your got 89 marks in exam.
Welcome Maxon
Your got 93.5 marks in exam.