• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Is it possible to override main method?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Is it possible to override main method?

Thanks in Advance
Athira
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic