• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any one please let me know ...

Can main method overloaded or overriden?

This is an interview question...


Please help.
Anu
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it can be overloaded & overridden
check this Example:
 
Karan Johar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you overload, then it is no longer the usual main method (i.e it cannot be invoked automatically by jvm)

it can be both overridden and overloaded.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karan Johar wrote:If you overload, then it is no longer the usual main method (i.e it cannot be invoked automatically by jvm)

it can be both overridden and overloaded.


then, is it possible to write like this...will the code compile without any error??

 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeas it compiles. But can not be run as main method.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.

Thanks Rahul, karan and Pankaj.

Regards,
Anu
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The normal public static void main(String[] args) method cannot be overridden, because it is a static method, and you cannot override static methods.

See http://faq.javaranch.com/view?OverridingVsHiding
and http://geekexplains.blogspot.com/2008/06/can-you-override-static-methods-in-java.html
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper is right, in some other topic, I've answered that in case of same signature static methods, it is not called overriding, it is called hiding and call to it is decided by the type off reference variable and not by the type of object instantiated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic