• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

static methods can't be overridden?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why all the rules of overriding are followed when we try to override(i mean redefine) static methods in sub-classes?

Here is an example and the compile error


Test.java:10: overrideMe() in Cat cannot override overrideMe() in Animal; attempting to use incompatible return type
found : boolean
required: void
public static boolean overrideMe(){
^
1 error

The compile error is contradicting the statement 'static methods can't be overridden'.

In this case if the method declaration matches, are we actually overriding a static method and not redefing?

'static methods' are inherited and but just does not support polymorphism(runtime binding).

So is it that static methods can actually be partially overridden, because they follow rules of overriding and but do not support runtime binding.
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, static methods aren't inherited. This question comes up frequently. Read this thread, this FAQ, this thread, and this thread. Searching for "static method overridden" will give lots more links.

I think those links may answer your questions; if not, please do ask again.

[edit]Spelling corrections[/exit]
[ May 29, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there I'm having the same doubt I have already read the threads posted here, but now one explains why the compiler still complains, I have tested another situation pls look at the following code:



and...



the compiler complains that the implemented method m4 is not being overridden properly it says "cannot reduce visibility of inherited method from AbstractTest

I'm clear about the concepts but no one is explaining why there are compilation errors like this... and nobody posted examples of static methods where overriding rules could be applied.. can somebody explain why the compiler thinks you are trying to override the static method?..

I'm using eclipse to compile and build, thanks.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

A static method cannot override an inherited instance method, but it can hide a static method if the exact requirements for overriding instance methods are fulfilled. A hidden superclass static method is not inherited. The compiler will flag an error if the signatures are the same but the other requirements regarding return type, throws clause, and accessibility are not met. If the signatures are different, the method name is overloaded, not hidden.


From khaled Moghal book.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Serch Hdez:
Hi there I'm having the same doubt I have already read the threads posted here, but now one explains why the compiler still complains, I have tested another situation pls look at the following code:



and...



the compiler complains that the implemented method m4 is not being overridden properly it says "cannot reduce visibility of inherited method from AbstractTest

I'm clear about the concepts but no one is explaining why there are compilation errors like this... and nobody posted examples of static methods where overriding rules could be applied.. can somebody explain why the compiler thinks you are trying to override the static method?..

I'm using eclipse to compile and build, thanks.




even if m4() wasn't static in AbstractTest(and in class A as well,obviously) , it would still result in a compiler error as m4() in class A has to be public(not protected) as overriding method's access level can't be more restrictive than the overridden method's.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Me too having the same doubt.....
Can someone please explain ?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

� 8.4.8.2 of the current version of Java Language Specfication defines that the same rules are applied for hiding static methods and overriding instance methods. Here is the link:

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#227929

regards

Serg
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

The most relevant link you gave for my question is this FAQ
But still my doubt is not clarified.

Please look at the example and the output:

class Cat extends Animal{
public static void main(String[] args) {
new Cat().overrideMe();
}
}

class Animal{
public static void overrideMe(){
System.out.println("in super class static method");
}
}


Output: in super class static method

It was contracdictory to what you quoted.

Originally posted by Campbell Ritchie:
No, static methods aren't inherited.



The main point of analysing

Why all the rules of overriding are followed when we try to override(i mean redefine) static methods in sub-classes?


comes from the fact that static methods are inherited.

And as static methods are inherited, when we try to override them, we have to follow all the rules of overriding from return types to declaring exceptions for there is already a method inherited with same name.

Ranchers please put in your views.
 
Serch Hdez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I did a test and remove the m4 method from the A class, and I was able to access the m4 static method in the abstract class by doing



So this shows for me that the method static is inherited, any thoughts on this?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is that static methods are inherited. However, because they cannot be overridden, they do not behave polymorphically.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are NOT inherited in the true sense of inheritance
Static methods are class methods and when we load a subclass,its superclass is automatically loaded.
Since static methods have a single copy available to the whole class hierarchy, so we can use it and it seems we are using inheritance.

For example if the method is not static then, after inheritance two separate copies are produced which are used according to the method call. But in the case of static, same method is called irrespective of the calling.
Instead of using a new className(), if you just use the dot operator with the class name ,it still compiles
[ May 30, 2008: Message edited by: Jitendra Jha ]
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jitendra Jha:
Static methods are NOT inherited . . .

Thank you. I think that answers Ram Manoj's question too.
 
Serch Hdez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jidentra do you have an explanation of why the compiler thinks and enforce the rules of overridding in static methods, on the super class with their subclasses?, thanks.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jitendra Jha:
Static methods are NOT inherited in the true sense of inheritance...


Well, maybe this depends on what you mean by the "true sense" of inheritance. (I've spend a lot of time searching the JLS for clarification on this, but haven't found anything.)

Deferring to an old EFH post...

They are inherited in the sense that they are in the scope of the subclass -- i.e., a static method Parent.method() can also be called as Child.method(), or just as method() withing the Child class.

 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The official statement from the language gods is that they are not inherited. But please note that this statement is only true for some specific definition of inheritance. That definition is not, unfortunately, the intuitive one. For example, the official word is also that private instance variables are also not inherited, but of course, by some definition they are: an instance of the Child class must contain all the instance data of the Parent class; they're just not in scope.

We have this argument over and over again, and it's silly, really, because it's not even useful. Instead of arguing over whether statics are inherited, it's much more useful to talk about their specific and observable properties. For example: are static methods polymorphic? The answer is no, and that's really easy to demonstrate. Or: can you call a static method defined in a parent class on an instance of a child class? The answer is yes, you can (and may people take that at an operable definition of inheritance especially if they come from a C++ background, where not all inherited instance methods are polymorphic.)
[ May 31, 2008: Message edited by: Ernest Friedman-Hill ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ernest! I suspected this had to do with how "inheritance" is defined.

It's interesting you mention the parallel argument regarding private members. I've found private members addressed under JLS - 8.2 Class Members...

Members of a class that are declared private are not inherited by subclasses of that class.


But I cannot find such a statement for static members. In fact, JLS - 8.4.8 Inheritance, Overriding, and Hiding seems to imply that non-private static methods are inherited, provided they are not hidden...

A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (�8.4.8.1) nor hidden (�8.4.8.2) by a declaration in the class.


I realize this is a somewhat arbitrary distinction, but I'm trying to understand the basis. Do you know of anything that clarifies this further?
[ May 31, 2008: Message edited by: marc weber ]
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
My understanding is that static methods are inherited. However, because they cannot be overridden, they do not behave polymorphically.



Hi Weber,
Rather delving deep, Can we say this,

Static methods are overridden for they follow rules of overriding for being inherited.
Static methods are not overridden in the sense of behaviour for they do not exhibit polymorphism.


And the official statement remains.

Originally posted by Ernest Friedman-Hill:
The official statement from the language gods is that they are not inherited. But please note that this statement is only true for some specific definition of inheritance.



And the language gods are not convinced to make assertions without sense of diplomacy.
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before i attempt to answer the question raised here, i would seriously like to know,what according to you is the definition of inheritance.
Unless and untill you understand the definition clearly,you will not be able to understand the static methods properties(related to inheritence).
I am waiting for an answer from all those of you who think static method's can be inherited.
I must again mention the point that static methods are independent of objects.
they exist in the system.
therefore it seems that they are inherited but in reality they are not.
i will certainly explain this in detail but only when i am certain that all of you have understood the definition.
please humour me by explaining inheritance( only those of you who are hell bent to prove that static method's are inherited)
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a family has a rich grandfather and that grandfather dies with a will.... the will specifies that the money from his estate is to go to build a neighborhood park with a lake, a walking/running path, a playground, a swimming pool.... everything that a community could want.

Did the family inherit it? No, not really.
Can the family use it? Of course they can. It was given to the community and the family is just as entitled to use it as anyone else. But, on the other side of the coin, other people in the community have just as much right to use is as the family. There is a relationship between the family and the grandfather who bequeathed the park.....but the family does not inherit it per se.... but they can use it.

The preceding philosophical moment has been brought to you by Bob.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a kind of binding .
static methods are binded at compile time according to
reference type .
Actual object referenced by Reference variable in not considered .

Always remember static members are related to early Binding .

 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nicely explained by BOB
i guess that explains it too well
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the late reply.

Thanks Jitendra and Bob.
For clarifying the true definition of inheritence and superbly with the analogy by Bob.

Don't think me otherwise.
Still something is itching my mind.
Are n't static methods when being redefined following the rules of overriding?

If it can be helpful/useful, please reply or close the discussion here.

Thankyou.
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just because it seems to be following some rules does not mean that it is overridden
that is the catch
be careful, as it seems to be overriding but is not
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Thanks Jitendra.

That sums it up.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic