Question: Can we Overload main() method?
Answer:
Case 1:
Overloading of the main method is possible but JVM will always call String[]
argument main method only. The other overloaded method we have to call
explicitly then it will be executed just as a normal method call.
Example:
Class Test{
public static void main(String[] args)
{
System.out.println(“String[]”);
}
//Overloaded Method
public static void main(int[] args)
{
System.out.println(“int[]);
}
}
Case 2:
Inheritance concept applicable for the main method.
Hence while executing child class if child class doesn’t contain main method
then parent class main method will be executed.
Class Parent{
Public static void main(String[] args)
{
System.out.println(“parent main”);
}
}
Class Child extends Parent
{
}
Case 3:
It seems overriding concept is applicable for main method but
it is not overriding, it is method hiding.
Class Parent{
Public static void main(String[] args)
{
System.out.println(“parent main”);
}
}
Class Child extends P{
System.out.println(“child main”);
}
Note: For main method inheritance and overloading concepts are applicable
but overriding concept is not applicable instead of overriding,
method hiding is applicable.