Method Overloading with Different Order of Parameters in Java


Write a Java program to demonstrate method overloading with different order of parameters

  • Class Definition: The Display class contains two overloaded methods named show. Method overloading allows multiple methods with the same name but different parameter lists to coexist within the same class. In this case, both show methods accept one integer and one string parameter, but in different orders.
  • Method Overloading: The first show method accepts an integer num followed by a string message, while the second show method accepts a string message followed by an integer num. This demonstrates method overloading based on parameter order.
  • Method Implementation: Inside each show method, the provided parameters are used to print a message to the console. The first method concatenates the num and message parameters with appropriate labels and prints the result. Similarly, the second method concatenates the message and num parameters with appropriate labels and prints the result.
  • Main Method: The main method is the entry point of the program. Inside the main method, an instance of the Display class is created using the constructor new Display() and assigned to the variable display.
  • Method Calls: Two method calls to the show method are made from the main method. The first call display.show(7, "Java") invokes the first show method with an integer 7 and a string "Java". The second call display.show("Hello World", 10) invokes the second show method with a string "Hello World" and an integer 10.

Source Code

class Display
{
	void show(int num, String message)
	{
		System.out.println("Number : " + num + ", Message : " + message);
	}
 
	void show(String message, int num)
	{
		System.out.println("Message : " + message + ", Number : " + num);
	}
 
	public static void main(String[] args)
	{
		Display display = new Display();
		display.show(7, "Java");
		display.show("Hello World", 10);
	}
}

Output

Number : 7, Message : Java
Message : Hello World, Number : 10

Example Programs