Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Static methods cannot be overridden

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'm full of tinier men! And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic