Forums Register Login

Static methods cannot be overridden

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
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
}}
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 810 times.
Similar Threads
Static methods and inheritance.
Accessibility of private methods in inner classes
Static methods cannot be overridden ?
Polymorphism Problem
Good question can you figure it out
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:36:03.