aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on "static methods cannot be overridden"" Watch "Question on "static methods cannot be overridden"" New topic
Author

Question on "static methods cannot be overridden"

Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
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?

For example, the following will not work:


The above gives error: "amethod in Super cannot override amethod() in sub; attempting to assign weaker access priviledges;...."

[ January 04, 2005: Message edited by: Richard Vagner ]

[ January 04, 2005: Message edited by: Richard Vagner ]
[ January 04, 2005: Message edited by: Richard Vagner ]
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
1. What is the reason behind "Static methods cannot be overridden"?

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

Joined: Jun 26, 2001
Posts: 107
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

Joined: Sep 29, 2003
Posts: 41
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

Joined: Jun 26, 2001
Posts: 107
Thanks Swapnil, but you are not really answering my question.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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

Joined: Jun 26, 2001
Posts: 107
Thanks Jay,

That really helps!
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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?

Well, I didn't design Java but I'll try to answer your questions.

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

Joined: Jun 26, 2001
Posts: 107
Thanks Mike, you are my hero! That is probably the best answer I can ever get.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on "static methods cannot be overridden"
 
Similar Threads
usage of super
static overriding
it should be more ambiguous then previous version.
Super-Sub classes
overriding method question and a super question.