aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes curious why static methods shawdowed don't act like variables? 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 "curious why static methods shawdowed don Watch "curious why static methods shawdowed don New topic
Author

curious why static methods shawdowed don't act like variables?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Just curious, if static methods are not really overridden in subclasses and are only really shadowed, why can't you make the accessbility modifiers more restrictive? It seems like if the static methods are really not overridden then they should behave more like varaibles that are really only shadowed.
For example:


I'm sure there is a logical good explanation, I just don't see it at the moment for static methods.
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
if static methods are not really overridden in subclasses

I don't see why this isn't overriding. (can't find this in the literature either).
In that perspective it just follows the rules that an overriding method can't be declared more private....
Erik Dark
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Erik,
here is some literature:
JLS 8.4.6 Inheritance, Overriding, and Hiding
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by Erik Dark:

I don't see why this isn't overriding. (can't find this in the literature either).
In that perspective it just follows the rules that an overriding method can't be declared more private....
Erik Dark

It's definitely not overriding. Try the following code:

[ February 11, 2002: Message edited by: Rick Reumann ]
Bhushan Kumar
Greenhorn

Joined: Jan 28, 2002
Posts: 7
Actually, we have the access to static method which is declared as Public in Outer class from class Test. In this case it won't allow you to hide it from sub class. see the following code it will compiles fine.

class Outer
{
public static int i = 5;
// public static void doStuff()
// { }
private static void doStuff() { }
}

public class Test extends Outer
{
private static int i = 6; //ok
//private static void doStuff() {} //nope
public static void doStuff() {}// this is ok
}
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Bhushan, you just flipped the modifiers around. I definitely understand it will compile correctly that way (althought still be careful because you haven't really overrid the static method in the subclass). Statics methods don't behave that way so I'm just curious why they don't act like variables which are shadowed in subclasses, in which case the subclass could have a static method with the same signature as the superclass yet it is more restrictive in accessibility. ( I know it doesn't work that way, I'm just curious if there is a reason why ).
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
JLS:
The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.

So Valentin, can I conclude from this that hiding and overriding both follow the rule of not narrowing the access modifier?
If so I wonder why you just gave me the lead to the correct answer (thanx for that) and didn't answer Rick's initial question...
Erik Dark
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
So Valentin, can I conclude from this that hiding and overriding both follow the rule of not narrowing the access modifier?

You are right. That rule is stated in 8.4.6.3 Requirements in Overriding and Hiding
If so I wonder why you just gave me the lead to the correct answer (thanx for that) and didn't answer Rick's initial question...

Because it is better that way. You learn more from doing it yourself
HIH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: curious why static methods shawdowed don't act like variables?