Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on "static methods cannot be overridden"

 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Swapnil, but you are not really answering my question.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Richard,
I had the same question few months back and here is the explanation. Check out this
link

Hope this helps ya.
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jay,

That really helps!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike, you are my hero! That is probably the best answer I can ever get.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic