How to overload and override main method in Java
Last Updated : 05 Apr, 2019
Improve
How to overload main method in java?
Method Overloading can be defined as a feature in which a class can have more than one method having the same name if and only if they differ by number of parameters or the type of parameters or both, then they may or may not have same return type. Method overloading is one of the ways that java support Polymorphism. Yes, We can overload the main method in java but JVM only calls the original main method, it will never call our overloaded main method. Below example illustrates the overloading of main() in java Example 1:// Java program to demonstrate
// Overloading of main()
public class GFG {
// Overloaded main method 1
// According to us this overloaded method
// Should be executed when int value is passed
public static void main(int args)
{
System.out.println("main() overloaded"
+ " method 1 Executing");
}
// Overloaded main method 2
// According to us this overloaded method
// Should be executed when character is passed
public static void main(char args)
{
System.out.println("main() overloaded"
+ " method 2 Executing");
}
// Overloaded main method 3
// According to us this overloaded method
// Should be executed when double value is passed
public static void main(Double[] args)
{
System.out.println("main() overloaded"
+ " method 3 Executing");
}
// Original main()
public static void main(String[] args)
{
System.out.println("Original main()"
+ " Executing");
}
}

// Java program to demonstrate
// Overloading of main()
public class GFG {
// Overloaded main method 1
public static void main(boolean args)
{
if (args) {
System.out.println("main() overloaded"
+ " method 1 Executing");
System.out.println(args);
// Calling overloaded main method 2
GFG.main("Geeks", "For Geeks");
}
}
// Overloaded main method 2
public static void main(String a, String b)
{
System.out.println("main() overloaded"
+ " method 2 Executing");
System.out.println(a + " " + b);
}
// Overloaded main method 3
public static void main(int args)
{
System.out.println("main() overloaded"
+ " method 3 Executing");
System.out.println(args);
}
// Original main()
public static void main(String[] args)
{
System.out.println("Original main()"
+ " Executing");
System.out.println("Hello");
// Calling overloads of the main method
// Calling overloaded main method 1
GFG.main(true);
// Calling overloaded main method 3
GFG.main(987654);
}
}
Original main() Executing Hello main() overloaded method 1 Executing true main() overloaded method 2 Executing Geeks For Geeks main() overloaded method 3 Executing 987654