Overloading main() method in Java
Overloading main() method in Java
Overloading Concept: Overloading is also a special feature of java. Overloading is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input paramaters.
Note that in both C++ and Java, methods cannot be overloaded according to return type.
// let's take a Java program with overloaded main()
import
java.io.*;
public
class
TestMain {
// Normal main()
public
static
void
main(String[] args) {
System.out.println(
"Hi Mindclues(from main)"
);
TestMain.main("Admin"
);
}
// Overloaded main methods
public
static
void
main(String arg1) {
System.out.println(
"Hi, "
+ arg1);
Test.main(
"Dear Mindclues"
,
"My Mindclues"
);
}
public
static
void
main(String arg1, String arg2) {
System.out.println(
"Hi, "
+ arg1 +
", "
+ arg2);
}
}
Overloading main() method:
In the main method overloading, Java did not needed extra-functionality. Apart from the fact that main() is just like any other normal method & can be overloaded in a similar manner, JVM always looks for the method signature to launch the program.
- The normal main method acts as an entry point for the JVM to start the execution of program.
- We can overload the main method in Java. But the program doesn’t execute the overloaded main method when we run your program, we need to call the overloaded main method from the actual main method only.
Now the output of above programs: HiMindclues
(from main) Hi,Mindclues
Hi, DearMindclues
, MyMindclues
post a comment