• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods cannot be overridden

 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I read Corey's article which says that static methods CANNOT be overridden.So I tweaked the code as below.



So if the static method myStatic() in the child class is not overridden why do I get the following error

myStatic() in Child cannot override myStatic() in Parent;
overridden method is static final


I am confused or may be I am thinking wrong !!! Help!!!


(No need to shout)
[ September 30, 2004: Message edited by: Barry Gaunt ]
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is why does the compiler treats the static method in the child class as OVERRIDDEN ? The JLS says static method in the child class hides the static method in Parent class.
So why doesn't it hide when the parent class static method is FINAL ?
 
Ozgur Nickpherly
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, error can be removed by deleting final keyword. Final functions can not be overrided whether they are object method or static method.
If object methods are overrided, you can not reach super method but static methods can be reached. See example below:

class Parent {
public static void myStatic() {
System.out.println("Parent");
}}
class Child extends Parent
{
public static void myStatic() {
System.out.println("Child");
}
public static void main(String[] args){
Parent p = new Child();
p.myStatic(); // prints Parent
Child c = (Child)p;
c.myStatic(); //prints Child
}}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have deleted my previous post because it was wrong.

Well here it is from the JLS:

8.4.3.3 final Methods
A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.



Notice the "or hiding". It does not matter if the method is static or not. Maybe the compiler implementor is reusing the error message used for overriding rather than using a specific error message for hiding.
[ September 30, 2004: Message edited by: Barry Gaunt ]
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Barry for that clarification. I was puzzled by the compiler's message.

Thank you to all who posted the message in regards to my query.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Barry is dead on. It's the compiler error that is so confusing. The error arises because you're trying to hide a final method, not override it. Static methods cannot be overridden, but they can be hidden. The keyword "final," however, prevents either from taking place.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic