Method Overloading with Methods That Have Different Parameter Modifiers, Including Final and Non-Final in Java


Write a Java program to demonstrate method overloading with methods that have different parameter modifiers, including final and non-final

  • In the main method of the OverloadingWithParameterModifiersDemo class, an instance of the MethodOverloadingDemo class is created.
  • Two variables message and number are initialized with a string and an integer value, respectively.
  • The printInfo method of the MethodOverloadingDemo class is called twice, once with a string argument and once with an integer argument.
  • The first call printer.printInfo(message) invokes the method with a non-final parameter, and it prints the string message.
  • The second call printer.printInfo(number) invokes the method with a final parameter, and it prints the integer value.

Source Code

public class OverloadingWithParameterModifiersDemo
{
	public static void main(String[] args)
	{
		MethodOverloadingDemo printer = new MethodOverloadingDemo();
 
		String message = "Hello World";
		int number = 67;
 
		printer.printInfo(message);
		printer.printInfo(number);
	}
}
class MethodOverloadingDemo
{    
	void printInfo(String message) // Method with non-final parameter
	{
		System.out.println("Printing Non-Final : " + message);
	}
 
	void printInfo(final int number)// Method with final parameter
	{
		System.out.println("Printing Final : " + number);
	}
}

Output

Printing Non-Final : Hello World
Printing Final : 67

Example Programs