• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we override or Overload Main mathod  RSS feed

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is one of the interview question which was asked to me today.

Can we override or overload a main method...If yes how and if no the why not.


Thanks in Advance
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, why not? There's nothing special about the word "main". Although remember that you can't override static methods.
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can. Here is the example. Hope you found the answer for your question.

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhash

You are not overriding the main method in the code snippet you have provided. You are simply calling the main method in the normal static way. No override occurs as static methods belong to the class, not an instance of a class and hence, cannot be overridden.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can overload the main method...but the JVM will only ever use the one with a String[] argument as the entry point for the application.
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you let me know in more detail Matthew


regards
deepak
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is that the following is perfectly valid code:

Here, main has been overloaded, just like any other method can be overloaded. When you run this class from the command line, you will only ever get "The main program is running" as output. The JVM will only look for a method with the exact signature public static void main(String[]). It will ignore any other main methods with a different signature.

However, it's perfectly possible to call the other method explicitly from elsewhere in the code, by calling TestMainOverload.main().

Does that help?

(I'd also add that, while you can do this, it doesn't mean that you should do this. In fact, you should avoid it - it's just likely to lead to confusion).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, as already mentioned, you can define a non-static method called main, and override it in a subclass:


But I wouldn't consider that as overriding the main method. When someone refers to the main method in a Java context, they're usually talking about public static void main(String[] args). That method can be overloaded, but, because it is static, it cannot be overridden.

So you can definitely overload the main method, and you can override a main method, but you can't override the main method (by the common usage of the phrase "the main method").

And of course, just because you can do something, it doesn't mean that you should do it. I've never run across a situation where it made sense to define main as anything other than public static void main(String[] args).
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You all.Its a Great Place
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!