my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding static methods" Watch "Overriding static methods" New topic
Author

Overriding static methods

dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hello.
Can static methods be overridden?
I wrote simple code to test. Given that the declaration type of the reference and the object are both of the subclass type: <subclass type> var = new <subclass type>(), the static method was overridden from its superclass. However, using dynamic method binding where: <superclass type> var = new <subclass type>(), the static method from the superclass was called instead of the one in the subclass.
For instance methods. The overriding methods in the subclass during dynamic method binding, as expected, was called.
I first had the impression if you can inherit it, then you can override it. Is this true for static methods?
Here's the code i used:

thanks.


SCJP 1.4<br />SCWCD 1.4
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Static (class) methods are not overridden, they are hidden or redefined in the subclasses. Polymorphism doesn't apply to static members, they are bound statically at compile time. The class type determines which static method will be called. Here's a set of valuable rules from JLS to follow while hiding methods:

8.4.6.3 Requirements in Overriding and Hiding
If a method declaration overrides or hides the declaration of another method, then a compile-time error occurs if they have different return types or if one has a return type and the other is void. Moreover, a method declaration must not have a throws clause that conflicts (�8.4.4) with that of any method that it overrides or hides; otherwise, a compile-time error occurs. In these respects, overriding of methods differs from hiding of fields (�8.3), for it is permissible for a field to hide a field of another type. The access modifier (�6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:
CLASSES Inheritance, Overriding, and Hiding 8.4.6
� If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.
� If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error
occurs.
� If the overridden or hidden method has default (package) access, then the
overriding or hiding method must not be private; otherwise, a compile-time
error occurs. Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
As Vad pointed out, static methods CAN NOT be overridden. Overriding is only involved with instance methods.


SCJP Tipline, etc.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Thanks Vad.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Corey. Yes, I clearly understood Vad's point. The JLS contains a lot of information I should have been reading. Thanks.
 
GeeCON Prague 2014
 
subject: Overriding static methods