JavaWhat is Method Overloading?

// Method overloading allows multiple methods in the same class to have the same name with different parameters.
public class Main {
    void display(int a) {
        System.out.println("Integer: " + a);
    }

    void display(String b) {
        System.out.println("String: " + b);
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.display(5);          // Output: Integer: 5
        obj.display("Hello");    // Output: String: Hello
    }
}

Related Snippets

Cover Image for Explain Inheritance in Java?

Explain Inheritance in Java?

Loading...
Cover Image for Generate a random number

Generate a random number

Loading...
Cover Image for How does Exception Handling work in Java?

How does Exception Handling work in Java?

Loading...
Cover Image for What is a List in Java?

What is a List in Java?

Loading...
Cover Image for What is the main method in Java?

What is the main method in Java?

Loading...
Cover Image for What is the Singleton Pattern?

What is the Singleton Pattern?

Loading...