Create a Java program to demonstrate method overloading with different parameter types and variable arguments
This Java program demonstrates method overloading in a class named Printer, where there are two print methods: one to print a single string and another to print an integer followed by multiple string messages. Let's break down the code:
class Printer { void print(String text) { System.out.println("String : " + text); } void print(int num, String... messages) { System.out.print("Integer : " + num + " , Messages : "); for (String message : messages) { System.out.print(message + " "); } System.out.println(); } public static void main(String[] args) { Printer printer = new Printer(); printer.print("Hello World"); printer.print(84, "Java is", "awesome"); } }
String : Hello World Integer : 84 , Messages : Java is awesome
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions