Overloading main Method in java

OverLoading Main:
This is a simple program to explain overloading and overriding concept in java with main method.

public class MyMainMethod
{
     // Overloading main Method

public static void main(String s[])
{
System.out.println("Main method");
main(10);
}
public static void main(int num)
{
System.out.println(num);
}

}
OutPut:
Main method
10
Explanation:
As per the program, we can see overloading main method i completely allowed and achievable.

As per java execution starts with main method with String array arguments.So JRE finds the default main method to start the execution , which is already differentiated by argument type with other Main method.

Overriding is not possible with main as it is static method because it is not considered overriding as there won’t be any run-time polymorphism.If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.



Comments

Popular posts from this blog

PHP+ MYSQL/MARIADB as Backend

Core java Notes and Learning