Method Overloading with a Mixture of Data Types and Parameter Counts in Java


Create a Java program to demonstrate method overloading with a mixture of data types and parameter counts

This Java program defines a class named Example with several overloaded display methods. Overloading means having multiple methods with the same name but different parameter lists.

  • The main method creates an instance of the Example class.
  • It then calls the display methods with different arguments.
  • The Java compiler determines which method to call based on the number and type of arguments passed.
  • For the first call, display(42), the int version of the display method is invoked.
  • For the second call, display(3.14), the double version of the display method is invoked.
  • For the third call, display("Value is ", 10), the String and int version of the display method is invoked.

Source Code

class Example
{
	void display(int num)
	{
		System.out.println("Integer : " + num);
	}
 
	void display(double num)
	{
		System.out.println("Double : " + num);
	}
 
	void display(String message, int num)
	{
		System.out.println("Message : " + message + ", Number : " + num);
	}
 
	public static void main(String[] args)
	{
		Example example = new Example();
		example.display(42);
		example.display(3.14);
		example.display("Value is ", 10);
	}
}

Output

Integer : 42
Double : 3.14
Message : Value is , Number : 10

Example Programs