In this post, we will look at whether we can overload or override the main method in Java? But first, we would recommend you read the articles about method overloading and method overriding to understand these concepts.
Let’s look at the above points one by one.
Can we overload the main method in Java?
Method overloading is a technique by which multiple methods can have the same name but with different numbers or types of parameters. And now the question is, can we overload the main method in Java?
The answer is Yes. We can easily overload the main method just like any other method but remember; the JVM will always call the original main method and never the overloaded ones, as shown in the below program.
public class Codekru {
public static void main(String[] args) {
System.out.println("Original main method");
}
public static void main(int args) {
System.out.println("Int args overloaded main method");
}
public static void main() {
System.out.println("No args overloaded method");
}
}
Output –
Original main method
Here, we can see JVM calls the original main method.
Then, how can we call the overloaded methods?
We will be able to call the overloaded methods from the original main() method in the same way we do with other methods.
public class Codekru {
public static void main(String[] args) {
System.out.println("Original main method");
main(1); // calling "int" overloaded method
main(); // calling "no-arg" overloaded method
}
public static void main(int args) {
System.out.println("Int args overloaded main method");
}
public static void main() {
System.out.println("No args overloaded method");
}
}
Output –
Original main method
Int args overloaded main method
No args overloaded method
Here, we were able to call the overloaded main() methods from the original main() method.
But we should avoid writing the main() method with the same parameters as the original main() method but with a different return type because that is no method overloading. The number of parameters or type of parameters must be different for overloading.
public class Codekru {
public static void main(String[] args) {
System.out.println("Original main method");
}
// this is not correct
// this is giving "Duplicate method name" error
public static int main(String args[]) {
System.out.println("return type is int");
}
}
Can we override the main method in Java?
If a subclass also defines a method present in the superclass. Then we can say that the subclass has overridden the superclass method, which is known as the method overriding. And here, we can only override the instance methods and not the static methods because static methods are bonded with the class instead of the object.
So, can we override the main() method in Java?
No, we cannot override the main() method in Java. This is because Java’s original main() method is marked as static and static methods cannot be overridden. You won’t get an error if you try to override the main() method. But that would be the method hiding and not method overriding.
public class Codekru {
public static void main(String[] args) {
System.out.println("Original main method");
Subclass subclass = new Subclass();
subclass.main(args); // this is "method hiding"
// and not "method overriding"
}
}
class Subclass extends Codekru {
public static void main(String[] args) {
System.out.println("main method in Subclass");
}
}
Output –
Original main method
main method in Subclass
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.