• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to override main method?  RSS feed

 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is it possible to override main method?

Thanks in Advance
Athira
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you mean the main method (and not just any old method called main) - main is a static method, and you can't override static methods. So no.
 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

But still i am in confusion

i have a class called MainClass


one subclass called that xtend main class



here the signature of main method is same that means main method got overriden .right?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. What you've got there is method hiding, not overriding.

The difference is, if you call MyMainClass.main, you'll get the version in MyMainClass. If you call OverridingMain.main, you'll get the version in OverridingMain - there are two separate methods, and you don't get any polymorphic behaviour.

It's called hiding because if you just call main in OverridingMain it will call that version, whereas if you deleted that main method it would call the one in MyMainClass - the existence of the method in the subclass has "hidden" the one in the superclass, which then has to be accessed via the class name.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In Java we can not override a method which is static, but we can implement the method with the same name and parameters. This is called shadowing and not overriding.

Hope this helps.
 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a class hierachy, when a method in a subclass has the same name and type signature as a method in its super class, then a method in the subclass is said to override the method in the super class.
When an overriden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.The version of the method defined by the superclass will be hidden.

In that sense main method is said to be overriding.right?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Athira Vineeth wrote:In that sense main method is said to be overriding.right?

No. As I said, overriding does not apply to static methods.

See this section of the Java Language Specification if you want the definitive answer (though it's not always easy to read). Section 8.4.8.1. about overriding refers to instance methods, and section 8.4.8.2. about hiding refers to static methods.
 
Athira Vineeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brown!!

that link is very helpful.

it is clear now
From this example

output is Goodnight, Dick

Thanks
Athira
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!