| Author |
Static method Overriding- confusion
|
Atul Shukla
Ranch Hand
Joined: Oct 09, 2006
Posts: 34
|
|
Can anyone please help me in clearing my doubts on Static method overriding. Confusion is : class a{ static void v(){} } class b extends a{ static int v(){} } it gives compile time error? why . I know the fact that we can redifine static methods in subclass but can't override ..and dynmic binding of methods will not work .. that's ok .. but why i can't redifine in the way i done above.. why java creators restricted that.... -Atul
|
 |
Atul Shukla
Ranch Hand
Joined: Oct 09, 2006
Posts: 34
|
|
and please let me know other facts about static method overriding that will help in SCJP. Thanks!!
|
 |
yogesh sood
Ranch Hand
Joined: Aug 31, 2000
Posts: 108
|
|
Hi Atul, Check link given below might be helpful for you. http://www.coderanch.com/t/259844/java-programmer-SCJP/certification/Static-methods-redefined-not-overridden
|
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
|
 |
Mike Ngo
Ranch Hand
Joined: Oct 16, 2006
Posts: 89
|
|
|
overriding method must have same return type.
|
 |
Illidan Yu
Greenhorn
Joined: Oct 21, 2006
Posts: 1
|
|
your static method in class b is neither overload or override method for the static method in class a. so there will be a compile time error.
|
 |
Mike Ngo
Ranch Hand
Joined: Oct 16, 2006
Posts: 89
|
|
|
Take the keyword "static" out and it will compile. The error is not about static method. It is because overriding method must have same return type.
|
 |
d jones
Ranch Hand
Joined: Mar 13, 2006
Posts: 76
|
|
Could anone please explain in more detail why there is a compiler error. I was surprised to see this since we are redefining the static method rather than overriding it. Thanks
|
 |
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
|
|
Hi ranchers, I want add a little to your confusion Once and again I read something like: "static methods are not inherited" or "static methods cannot be overidden, only redefined". I won't say, these statements are not totally wrong, but quite misleading. These statements only want to express that static methods don't show late binding. But in a way, both statements are only half true, if not false. I won't say much about the first, as it has not much to do with Atul Shukla's question. But I would say, no, static methods ARE inherited, otherwise would not print "Hi cowboys!". To the second: "static methods cannot be overidden, only redefined". I also used to say "redefinition" when speaking about static methods, (as for example in the link provided by Yogesh). But now I reject the term "redefinition" for static methods. Why? For example, if you try to compile The compiler says: foo() in B902 cannot override foo() in A901; overridden method is static final The code would compile if you delete the final modifier. It would even compile (in Java 5) if you add the override annotation: In my opinion, you could or even should say, that static methods CAN be overridden. First, the compiler has the same opinion Second, the rules for overriding non-static methods are the same as for static methods. I mean concerning e.g. return types, covariant returns, exceptions and accessibility modifiers. And as you all have to care about all of these, how you'd say, that static methods aren't overridden? The fact, that static methods do not show late binding is not a reason to rename "override" with "redefine" for static methods. I would use the term redefinition only for private methods, that are not visible in subclasses and thus can be redefined. And if we assume, that static methods can be overidden, the answer to Atul's question is easy and has been answered by the compiler already: v() in a cannot override v() in b; attempting to use incompatible return type Yours, Bu.
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
I think the compiler error message is misleading. It is clearly stated in the Java Language Specification that static methods are hidden, not overridden, in subclasses. To get to the original question, this is from the Java Language Specification 8.4.8.3 8.4.8.3 Requirements in Overriding and Hiding If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type substitutable for d2, or a compile-time error occurs. Furthermore, if R1 is not a subtype of R2, an unchecked warning must be issued (unless suppressed (�9.6.1.5)). A method declaration must not have a throws clause that conflicts (�8.4.6) with that of any method that it overrides or hides; otherwise, a compile-time error occurs.
|
 |
 |
|
|
subject: Static method Overriding- confusion
|
|
|