Can you Overload or Override main method in Java? Example

One of the common doubts among Java beginners while learning the overloading and overriding concept is, whether it's possible to overload the main in Java? Can you override the main method in Java? How will JVM find if you change the signature of the main method as part of the method overloading? etc. These are good questions and show the curiosity and application of knowledge of students, so if you are the tutor you must answer these questions. If you don't know the answer to this question, don't worry, just read this article till the end and you will find the answer yourself.  I have tried to explain in simple words but if you still can't get it, feel free to comment and I will try to help you personally. 
The short answer to, can we overload the main method in Java is Yes, you can overloading, nothing stops from overloading, but JVM will always call the original main method, it will never call your overloaded main method. 

You will learn this in little more detail later, now coming to the next question, can you override the main method in Java? the answer is No because the main is a static method and the static method cannot be overridden in Java.

In order to answer this question understanding of overloading and overriding is necessary. You should know that overloading takes place at compile time and overriding takes place at runtime. You should also be familiar with the rules of method overloading and overriding in Java.

Though main() is a special method because it's the entry point of your Java application, it follows the same rule of method overriding and overloading as any other method. In this article, you will learn how to overload the main method to see whether JVM calls the original, standard main with string argument or not.




Overloading the main method in Java

Here is how you can overload the main method in Java. This is just one way, you can create as many versions of the main as you want, but you must make sure that the method signature of each main is different. 

You can change the method signature by changing the type of argument, the number of arguments, or the order of arguments. 

The best practice to overload a method or constructor is by changing the number of arguments, or types of arguments. Overloading methods just by changing the order of arguments may create confusion and increase the chance of calling the wrong method. It's worth remembering that,

In Java, it's not possible to overload the method by just changing the return type.

Can we overload main method in Java




Code Example of Overloading Main Method in Java

/**
 * Java Program to show that you can overload main method in Java
 * but you cannot override main method.
 * 
 * @author Javin Paul
 */
public class Helloworld {

    /**
     * Standard main method, JVM will only call this method
     * even if you provided multiple overloaded version.
     * 
     */
    public static void main(String[] args) {
        System.out.println("Inside main(String[] args) method ....");
       
    }
    
    /**
     * An overloaded main method which accepts Integer[] instead of
     * String[] as argument. 
     * @param args
     */
    public static void main(Integer[] args){
        System.out.println("Inside main(Integer[] args) method ....");
    }
    
    
    /**
     * Another overloaded main method which accepts Double[] instead of
     * String[] as argument. 
     * @param args
     */
    public static void main(Double[] args){
        System.out.println("Inside main(Double[] args) method ....");
    }

}

Output
Inside main(String[] args) method ....

In this example, you can see that we have two main methods, one accepts String array as an argument and the other accept Integer array as an argument, but you can see that when you run our program from the command line, only the main method with string array as an argument is called.

There was no error, no ambiguity, JVM will always call this main method, no matter how many overloaded main methods you will put on this class. Then questions come how do you call your overloaded main? Well, you can call it just like any other method.

Simply calling main(new int[]{1, 2, 3}) from the original main method will invoke your overloaded main with integer array as an argument. Since there is no compilation error, it proves that you can overload the main method in Java.

Regarding Overriding we have already proven that the static method cannot be overridden, they can only be hidden. See that post to learn this concept by following an example. In short, the main method can be overloaded but cannot be overridden in Java.

Main method can be overloaded but cannot overridden in Java


That's all about overloading and overriding the main method in Java. Now you know that it's possible to overload main in Java but it's not possible to override it, simply because it's a static method. Execution of Java program has no impact on overloading main because JVM always calls the original main method and if it doesn't found in class then it throws java.lang.NoSuchMethodError: main Exception in thread "main" error at runtime.


If you like this tutorial and looking for some more Java beginners tutorials, I suggest checking out the following tutorials from this blog :
  • 130+ Java Interview Questions from the last 5 years (questions)
  • Understanding the Helloworld Program in Java (explanation)
  • How to run a Java program from the command prompt? (example)
  • 10 things Every Java programmer should know about the main method (article)
  • Can you run a Java program without the main method? (answer)
  • What is the difference between PATH and CLASSPATH in Java? (answer)
  • What is a .class file in Java? How does it work? (answer)
  • 40 Binary Tree-Based Algorithms Questions (BST problems)
  • Top 5 Object Oriented Design Interview Questions (OOP questions)
  • How to make executable JAR in Eclipse? (steps)
  • 20+ System Design Questions for Coding interviews (design questions)
  • Difference between Overloading, Overriding, and Shadowing (answer)
  • How to run a Java program from the JAR file? (example)
  • 10 Best System Design Courses for Interviews (system design courses)
  • The Complete Java Developer RoadMap (guide)
  • Top 30 Linked List Interview Questions (linked list problems)

Thanks for reading this article so far. if you find this Java interview question useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 

P. S. - If you are new to the Java world and looking for a free online course to start learning Java then I highly recommend you check out this Java Tutorial for Complete Beginners(FREE) course by John Purcell on Udemy. It's completely free and you just need an Udemy account to join this course. 

16 comments:

  1. Why do someone need to overload main? forget about overriding it? I have never seen a scenario, where you need to overload main() in Java.

    ReplyDelete
    Replies
    1. This situation may not arise, But Its the flexibility that java provides for overloading main.

      Delete
  2. can you override main method in java?

    ReplyDelete
    Replies
    1. No, you cannot override main method in Java because its static, its bonded at compile time, so it only look at the type of class as object is available at runtime. When you create a similar static method in subclass, that is called method hiding.

      Delete
  3. Replies
    1. No Ramya, you cannot override main() in Java. Please see my explanation above.

      Delete
    2. No , can not override the main method in Java because of static modifier in java.

      Delete
  4. Can we override main()?

    ReplyDelete
    Replies
    1. No, you cannot override main method because its a static method in Java which belongs to class and cannot be overridden.

      Delete
  5. It is possible to override and overload main method.

    ReplyDelete
    Replies
    1. are you saying, its possible to override main method in Java? can you explain as I don't think its possible.

      Delete
  6. In some questions I answered No and marked as wrong and the explaination said that NO was the right answer lol

    ReplyDelete
    Replies
    1. Hello Juan, which question was that, I will fix it. This is the first time I am trying to add quizzes into my article, do you think its worth it? do you find it useful?

      Delete
    2. Two questions with overriding concepts had wrong answers, I have corrected it, can you try again?

      Delete
  7. Quiz is an excellent addition.. please add more questions over time to each section.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.