Write a Java Program to perform method Overloading.

Q.12 Write a Java Program to perform method Overloading.

Solution :- 

class PrintLine {
// Method to print a line of 40 asterisks (*)
static void printLine() {
for (int i = 0; i < 40; i++) {
System.out.print(“*”);
}
System.out.println();
}

// Method to print a line of ‘#’ characters, repeated `n` times
static void printLine(int n) {
for (int i = 0; i < n; i++) {
System.out.print(“#”);
}
System.out.println();
}

// Method to print a line of a specified character `ch`, repeated `m` times
static void printLine(char ch, int m) {
for (int i = 0; i < m; i++) {
System.out.print(ch);
}
System.out.println();
}
}

public class PolyDemo {
public static void main(String[] args) {
// Calling different overloaded methods
PrintLine.printLine(); // Prints 40 asterisks (*)
PrintLine.printLine(30); // Prints 30 ‘#’ characters
PrintLine.printLine(‘+’, 20); // Prints 20 ‘+’ characters
}
}

Output :- 

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.