Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding main

 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way static methods cannot be overridden by default, so why compiler error?


Because the rule for overriding and redefining is same.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final classes cannot be extended

final methods cannot be overridden by a subclass
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic