wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Static! 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!" Watch "Overriding Static!" New topic
Author

Overriding Static!

Shivey Upadhyay
Greenhorn

Joined: Oct 05, 2007
Posts: 29
Hi,
As we know that, a static method CANNOT be Overridden. We call static methods with the class name.
If we write a program as below:

class sTest29{
public static void fun(){
System.out.println("It is from sTest29");
}
}
public class StaticTest1 extends sTest29{
static void fun(){
System.out.println("It is from StaticTest1");
}
public static void main(String[] args){
fun();
sTest29.fun();
}
}
It gives cannot reduce the visibility of inherited method.


Thanks,
Shivey
SCJP, SCWCD, and Aspiring SCDJWS
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Go through this and if you dont understand let me know
OverrideStaticMethods

Thanks
Deepak


SCJP, SCWCD, SCBCD
Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23


I read the difference between overriding and hiding but I still have the same question as the original post.

Why that compile error on the second fun()? Does the compiler know about hiding? If so, why is the error about inheritance?
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
The problem is not with static method hiding. It is about visibility.
You cannot override a method and make it less private. In the superclass, fun() is declared public, whereas, in the subclass, there is no modifier(default access). The following code will not give compile error.


If you declare fun() in superclass as no modifier(default access), you can have fun() in subclass as default access or public. You can override/hide to make it more public but not less private. Hope this helps.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Karen]: Why that compile error on the second fun()? Does the compiler know about hiding? If so, why is the error about inheritance?

Unfortunately, the compiler sometimes gives misleading messages using incorrect terminology. In this case, the compiler says "cannot override fun() in sTest29; attempting to assign weaker access privileges". The compiler should not talk about overriding at all here - this is a mistake. The compiler is correct that this code is in error, but the explanation is incorrect. The Java Language Specification is very clear here, and since we're talking about a static method, "overriding" does not apply. At all.


"I'm not back." - Bill Harding, Twister
Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
Thanks! I had solidified the rule in my head about not being able to override static methods and this one almost muddied it a little.

But that makes sense. Hope that helped you too, Vidyha, to understand why in this case, the access modifiers aren't related to the error. You are right though, if they were NOT static, the subclass appears to be narrowing access which is also not allowed.
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Jim, I didn't quite understand the answer. Can you explain with the corrected code and where I am wrong?
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
Vidhya Ramaswamy

Actually you did not understand the question.The question was--if static methods are not overriden then why does the compiler give an error related to overriding? The compiler caught the error but could not correctly interpret the mistake and thus gave a misleading answer as pointed by jim


Jitendra
SCJP1.5
SCWCD1.5
Shivey Upadhyay
Greenhorn

Joined: Oct 05, 2007
Posts: 29
Thanks! EveryOne.
So, we conclude by saying that compiler did not gave right interpretation, as well as static methods CANNOT be Overridden(rule is a rule), however, they can be hidden.
Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
Actually, as it turns out, Vidhya is correct. If you switch the modifiers to be valid, the code compiles and runs correctly:



The rule about not overriding static methods should be demonstrated by what gets called in a polymorphic declaration as added to the main method there (not seen via compiler errors).

With these methods marked as static, the compiler prints out WHAT? from the super class. If you remove the static keywords and the static calls, the compiler will find the actual object type and print out BLAH.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Karen]: Actually, as it turns out, Vidhya is correct.

Right, and I never meant to imply otherwise. Vidya was addressing why there is an error, while I was addressing the fact that the compiler used incorrect terminology while reporting the error. There was no contradiction - we were just talking about different aspects of your question.
Karen Marie
Greenhorn

Joined: Nov 10, 2007
Posts: 23
Right. Got it. So in other words, to answer your question, Vidhya, you were not wrong.

Thanks guys! My test is supposed to be next week. Ack!
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
ok, got it. I actually got confused and couldn't understand what went wrong!
All the best, Karen for your test, and let us know your experience. I am taking mine next month.
Hasitha Randika
Ranch Hand

Joined: Sep 04, 2007
Posts: 50
i think you have got it incorrectly the truth is you can override the static method but its useless.

first you must remember the 5 rules of overriding

wider or same access range
same method name
same return type
same argument
narrower checked exception

so according to the first rule the compiler will throw a compile error at line three.

because first you have declared your static method using public modifier
so if you override the method it must be public not anything else.

remember this thing also in polymorphysm static methods goes to the referance type
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[HR]: i think you have got it incorrectly the truth is you can override the static method but its useless.

(A) It's not overriding. Calling it overriding just confuses people.

(B) It's not useless, though it is less powerful than true overriding.

See Overriding vs Hiding for more info.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding Static!
 
Similar Threads
Please explain the output of this !!!!
static
Method overriding and hiding problem.
Static Methods
Why it is illegal to use abstract and static modifiers together in method declaration ?