Java Program To Implement Method Overloading

Chapter: Miscellaneous Last Updated: 16-04-2023 12:42:00 UTC

Program:

            /* ............... START ............... */
                
public class MethodOverloadingExample {
    public static void main(String[] args) {
        MethodOverloadingExample obj = new MethodOverloadingExample();
        obj.print();
        obj.print(10);
        obj.print("Hello, World!");
    }

    // Method with no parameters
    void print() {
        System.out.println("This is an example of method overloading");
    }

    // Method with an integer parameter
    void print(int num) {
        System.out.println("Number: " + num);
    }

    // Method with a string parameter
    void print(String str) {
        System.out.println("String: " + str);
    }
}

                /* ............... END ............... */
        

Output

This is an example of method overloading

Number: 10

String: Hello, World!

> The first print() method is called with no arguments, so it simply prints the message "This is an example 
  of method overloading" to the console.

> The second print() method is called with an integer argument of 10, so it prints the message "Number: 10" 
  to the console.

> The third print() method is called with a string argument of "Hello, World!", so it prints the message 
  "String: Hello, World!" to the console.

> Note that each of these three print() methods has the same name, but Java is able to distinguish between 
  them based on their parameters. This is the essence of method overloading - having multiple methods with 
  the same name, but different parameters, so that we can use the same name for related functionality.


Notes:

  • This program demonstrates the concept of method overloading in Java. Method overloading is a way to define multiple methods in a class with the same name, but with different parameters. In this program, we define three methods called print() with different parameter lists.
  • The first print() method takes no parameters and simply prints a message to the console. The second print() method takes an integer parameter and prints the value of that parameter to the console. The third print() method takes a string parameter and prints the value of that parameter to the console.
  • In the main() method of the program, we create an instance of the MethodOverloadingExample class and call each of the three print() methods with different arguments. Because each print() method has a different set of parameters, Java is able to distinguish between them and call the appropriate method based on the arguments we pass in.
  • This program demonstrates the flexibility and convenience that method overloading provides in Java. By defining multiple methods with the same name but different parameters, we can use the same method name for related functionality, which can make our code more concise and easier to read.

Tags

Java Program To Implement Method Overloading #How to implement method overloading in java

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018

1