Question on "static methods cannot be overridden"

Richard Vagner

Ranch Hand

Posts: 108

posted 11 years ago

1. What is the reason behind "Static methods cannot be overridden"? 2. Static methods cannot be overridden but can be redefined. If that is the case, why the compiler still enforcing the overridding rules?

Since static methods are class members which will be decided at the compile time.

2. Static methods cannot be overridden but can be redefined. If that is the case, why the compiler still enforcing the overridding rules?

Static methods can be redefined, but with different parameters (i.e You can overload)

Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)

Richard Vagner

Ranch Hand

Posts: 108

posted 11 years ago

Thanks! But I do not think static methods have to be redefined with different parameters.

Again my question if the static method in subclass is not overriding the static method in the Super class, why compiler cares about overridding rules?

Swapnil Sapar

Ranch Hand

Posts: 41

posted 11 years ago

First, you have missed the word 'static' in the Super class Recorrecting it..

This will result in the compilation error you have mentioned "attempting to assign weaker access privileges; was public"

Because this is an attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.

In overriding (in your case redefining) you can change the access only in the following order private -> default(package) -> protected -> public

So adding 'public' to the Sub's signature will compile the code.

~Swapnil [ January 05, 2005: Message edited by: Swapnil Sapar ]

SCJP 1.4 (90%), SCWCD 1.4 (92%)

Richard Vagner

Ranch Hand

Posts: 108

posted 11 years ago

Thanks Swapnil, but you are not really answering my question.

Jay Pawar

Ranch Hand

Posts: 411

posted 11 years ago

Hey Richard, I had the same question few months back and here is the explanation. Check out this link

Hope this helps ya.

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.

Richard Vagner

Ranch Hand

Posts: 108

posted 11 years ago

Thanks Jay,

That really helps!

Mike Gershman

Ranch Hand

Posts: 1272

posted 11 years ago

1. What is the reason behind "Static methods cannot be overridden"? 2. Static methods cannot be overridden but can be redefined. If that is the case, why the compiler still enforcing the overridding rules?

Overriding in Java means execution-time selection of which method to run based on the type of the object on which the method was invoked. Since static methods are not invoked on objects, we can't override them. However, the reasons for the consistency rules as to return value, throws clause, and access mofifier are just as valid for static methods, so Java enforces thm anyway.

The main reason for these rules is that a subclass type "is-a" superclass type, so a method in a subclass should not violate the contract between the client and the superclass. This means returning what the client expects, not throwing exceptions the client isn't prepared for, and not restricting access that the client may be relying on. The argument may be weaker here because you can't unknowingly invoke a subclass's static method using a superclass name or reference, but suppose you didn't realize that a particular static method was hidden by a subclass method and you invoke the method using the subclass name, you would probably want behavior consistent to that of the superclass method.

A bit of a stretch, but it's the best reasoning I can see. If I ever meet James Gosling, I'll ask him his reasoning.

Mike Gershman
SCJP 1.4, SCWCD in process

Richard Vagner

Ranch Hand

Posts: 108

posted 11 years ago

Thanks Mike, you are my hero! That is probably the best answer I can ever get.