| Author |
overriding main
|
joshua antony
Ranch Hand
Joined: Jun 05, 2006
Posts: 117
|
|
Why am I getting compilation error in main method of Another. Any way static methods cannot be overridden by default, so why compiler error?
|
 |
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
|
|
Any way static methods cannot be overridden by default, so why compiler error?
Because the rule for overriding and redefining is same.
|
Asking Smart Questions FAQ - How To Put Your Code In Code Tags
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
You only get the error because you are using final on the base class static main() method. By using final you are forbidding the subclass from hiding the static method.
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
final classes cannot be extended final methods cannot be overridden by a subclass
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Kameron McKenzie: final classes cannot be extended final methods cannot be overridden by a subclass
Yes, both are true, but what is the relevance to the current question?
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
Sorry. Should have been more clear. Final methods cannot be overridden. By overriding a final method defined in a parent class, you will see the following error: "Cannot override the final method from StaticOverride" Once a method has been declared as being final, a subclass cannot re-implement that method by coding a method with the same name and argument signature. How could we fix the problem? 1) eliminate the keyword final from the main method in the parent class: public class StaticOverride { /*final keyword removed*/ public static void main(String[] args) { } } class Another extends StaticOverride { public static void main(String[] args) { } } 2) we could avoid using inheritance with the subclass, eliminating the parent/child relationship with the two classes. public class StaticOverride { public final static void main(String[] args) { } } /*Another no longer extends StaticOverride*/ class Another { public static void main(String[] args) { } } Hope that's a more detailed answers. Regards!
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
I think Barry's point is that the term overriding doesn't apply to the question because you are dealing with static methods.
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
|
|
Oh, I see what you're saying. I've got to stop looking inside that box. I treated the symptom, but not necessarily the cause. So, they symptom has gone away (the compile error), but I avoided the fundamental cause. Sometimes getting rid of symptoms is good too, though. Good thing I'm a Java Hack, and not a medical practitioner.
|
 |
 |
|
|
subject: overriding main
|
|
|