Interfaces for Custom Message Formatting in Java


Write a Java program to demonstrate the use of an interface for custom message formatting

  • The MessageFormatter interface defines a single method format(String message) for formatting messages.
  • In the main method of the Main class, an instance of the MessageFormatter interface is created using a lambda expression that appends "Formatted Message : " to the original message.
  • The original message is formatted using the format method of the MessageFormatter interface.
  • The formatted message is printed to the console.

Source Code

interface MessageFormatter
{
	String format(String message);
}
 
public class Main
{
	public static void main(String[] args)
	{
		MessageFormatter formatter = message -> "Formatted Message : " + message;
 
		String originalMessage = "Hello Wrold! 123";
		String formattedMessage = formatter.format(originalMessage);
 
		System.out.println(formattedMessage);
	}
}

Output

Formatted Message : Hello Wrold! 123

Example Programs