Can we overload or override main method in Java

Hello Friends,

In this tutorial, we will see whether we can overload or override main method in Java. This is very common interview question as well and the answer is really simple, if we don't overthink :)




To understand What is main method and what it does thoroughly, please read What does main method do in Java? Why main method is public,static,void?

Can we overload main method in Java?

Short answer is YES, we can overload main method in Java.

main method's specialty is that it provides starting point for a standalone java application. It's signature is such that JVM understands that signature and calls main method with specific signature.

main method signature is as follows :
public static void main( String[] args );
which means if we define another method in same class with another signature, JVM will still call main method with above mentioned signature only.
So it is perfectly fine to have another main method in same class with different signature and we very well know that methods with same name and different signatures within a class are known as overloads.

Let us see it working with example.

public class Application {
    public static void main(String[] args) {
        System.out.println("In JVM called main method");
        System.out.println("Before calling overloaded main method");
        System.out.println("Result of calling overloaded main method from JVM called" + "main method:" + main(10, 20));
    }

    public static int main(int a, int b) {
        System.out.println("In overloaded main method");
        return a + b;
    }
}
Output :
In JVM called main method
Before calling overloaded main method
In overloaded main method
Result of Calling overloaded main method from JVM called main method:30
So we can see from above example that 
- When we ran Application.java ,JVM called main method with following signature only

public static void main( String[] args );
   - We can have overloaded version of main method also. Here we had overloaded main  method with following signature
public static int main(int a,int b);
  - We can call overloaded main method just like any other method. In our example we called overloaded main method from JVM called main method as below
System.out.println("Result of calling overloaded main method from JVM called" + "main method:"+main(10,20));
  So with this we learnt that main method just like any other method can be overloaded. Now let us see whether it is possible to override main method in java.

Can we override main method in java ?

Short answer is NO, we can not override main method in java.
Reason is very simple. As main method is static and we know very well that we can not override static methods in Java, hence main method could not be overridden. So what will happen if we will try to override main method in subclass.

Let us see with an example.
public class Application {
    public static void main(String[] args) {
        System.out.println("Application class main method");
    }
}
public class AnotherApp extends Application {
    public static void main(String[] args) {
        System.out.println("AnotherApp main method");
    }
}
public class TestClass {
    public static void main(String[] args) {
        Application application = new Application();
        application.main(new String[1]);
        Application application1 = new AnotherApp();
        application1.main(new String[1]);
    }
}
Output :
Application class main method
Application class main method

So we can see from above example that 
- We defined main method in Application.java
- AnotherApp class extended Application class
- AnotherApp class also defined main method with exactly same signature.But we should note here
  that main method defined here in AnotherApp class is just another method and it is not a overriding
  Application class's main method.
- To prove that main method of AnotherApp is not overriding Application class's main method,we created
  TestClass, in which we tried to call main method's of Application and AnotherApp polymorphically as below

Application application = new Application();
application.main(new String[1]);
  
Application application1 = new AnotherApp();
 // In case main method would have overridden, main method of AnotherApp would have got called through below method call.
 application1.main(new String[1]);
But as we can see in output ,in both calls ,main method of Application class is called.

So with this we learnt that main method can be overloaded but can not be overridden in Java.

If you have any comment on the post, please post in comments section.

Thanks for reading!!!