File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding main" Watch "overriding main" New topic
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: 4968
    
    1

final classes cannot be extended

final methods cannot be overridden by a subclass
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: 4968
    
    1

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: 2367
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: 4968
    
    1

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overriding main
 
Similar Threads
Static Member Override???
use of super,this in static method
Doubt in Overriding Static Method....?
use of super,this in static method
Static methods