Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main() Overloading Vs Main() Overridding

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the below clarifications.I have gone through various web sites but did not find "CORRECT ACCEPTED ANSWERS" for the below Questions.






I'm pretty much

Please clarify the above concepts to me..
Highly appreciate if you can clarify these concepts to me...

--
Deepak Lal
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the going gets tough . . . Deepak Lal tells us why he thinks a static method can be overridden. We have a suitable FAQ.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty much convinced with the FAQ Camphell Ritchiee but if example's for Main() method Overloading are provided..it would be helpful.

The FAQ clearly states that
1>static methods "cannot be overridden".They can be "HIDDEN" i.e a static method in superclass will hide another static method in subclass.and hence static method in super class will be executed.(example:foo.classMethod())
2>static methods are class methods.
3>static methods do not support run time polymorphism.
4>static methods are called at compile time.

Please correct me for the above interpretations if I'm wrong..

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading is a basic concept of object-oriented programming. Doesn't the book or tutorial you're learning with provide examples of it?
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Mr Ulf Dittmer (sheriff) i do agree with you about concepts of overloading.but my query is different



Can a public static void main method in java be overloaded as shown below ?
I,e



Can i have something like above stated.what will happen when i execute the above program?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is actually pretty obvious what will happen if you execute that application; have you tried it?

If you can't understand why it behaves like that, and can't work out how to get it to behave differently, then please ask again.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi campbell richtie,


campbell richtie's Comments :

It is actually pretty obvious what will happen if you execute that application; have you tried it?

If you can't understand why it behaves like that, and can't work out how to get it to behave differently, then please ask again.




My Interpretation:




Now my question to the above output is why is it printing
"Welcome to Sydney and (not Welcome to Bangalore)" -- Any Comments Please.?


 
Henry Wong
author
Marshal
Pie
Posts: 21398
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now my question to the above output is why is it printing
"Welcome to Sydney and (not Welcome to Bangalore)" -- Any Comments Please.?



Because the JVM is call the main() method with a particular signature, and the "Welcome to Sydney" one matched that signature.

Henry
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate on what Henry said, this has nothing to do with overloading - the JVM has hard-coded logic that tells it to call "public static void main (String[])" at application startup.

That's different from calling a method from within Java code (where overloading and type matching rules do apply).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic