Author Topic:   need help!!
uma
unregistered
posted March 03, 2000 12:07 PM           
I know that instance method can be overriden to be more public can static methods be hidden to be more public.

class X
{
protected static void st()
{
System.out.println("X");
}
}

class Y extends X
{
public static void st() //It's not giving any error
{
System.out.println("Y");
}
}

I thought hiding a static method means you should keep the exact signature.

Can somebody throw some light on this.

Joe Java
ranch hand
posted March 03, 2000 12:34 PM             
As far as I understand, the accessiblity modifier is NOT part of the signature. The signature "is comprised of only the name of the method and the types and order of the parameters in the parameter list", to quote from Mughal and Rasmussen. Interestingly, the return type is not considered part of the method signature yet this must also match the return type of an overridden method. Regards, Joe

uma
unregistered
posted March 03, 2000 01:31 PM           
Joe, if accessiblity modifier is not part of the method signature howcome it's giving me the error if i changed the modifier from protected(superclass) to private in the subclass.

Thanks.

monty
greenhorn
posted March 03, 2000 01:32 PM             
I agree. The access modifier is not part of a method signature.

Put it simple:
1. OVERLOADING - the same method name, different agrement list and/or return type. Overloading just with a different return type is illegal.
2. OVERRIDING - re-rwite the parent class's method signature exactly.
3. Method access modifiers when overriding can NOT be more private.
4. Exceptions thrown in overidden methods can NOT be more restrictive or can not throw checked exceptions of classes which are not part of the orginial method.

Hope this helps

[This message has been edited by monty (edited March 03, 2000).]

[This message has been edited by monty (edited March 03, 2000).]

uma
unregistered
posted March 03, 2000 02:26 PM           
Thanks joe & monty.

Ramana Namuduri
greenhorn
posted March 03, 2000 05:02 PM             
Uma, I would like to higlight two concepts here.
1. Any static method must be hidden by only a static method. A static method cannot be hidden as non static. In the same way, a non static method cannot be overridden as a static one.
2. Regarding access modifiers, follow this rule,
private-> default -> protected -> public.
Here, default means no access modifier specified.
Now, a private method can be overridden to a default or protected or public. Follow from left to right and right to left is not possible.
I hope ur doubt got solved now,
Rgds

[This message has been edited by Ramana Namuduri (edited March 06, 2000).]

uma
unregistered
posted March 04, 2000 08:16 PM           
Thankyou ramana.

maha anna
bartender
posted March 04, 2000 08:57 PM             
Ramana,
private methods CAN NOT be overridden by subclasses. Subclasses do not even know that such a method exists in its parent. If at all any method defined is in subclasses with the same name as a private method in its parent , it is completely a new method.
regds
maha anna

[This message has been edited by maha anna (edited March 04, 2000).]

Greg Torrance
greenhorn
posted March 06, 2000 06:12 AM             
I'm confused now ...

Ramana said:
1. Any static method must be overridden by only a static method. A static method cannot be overidden as non static. In the same way, a non static method cannot be overridden as a static one.

But I thought static methods could not be overridden. [But they can be hidden, right?]

Uma asked:
Joe, if accessiblity modifier is not part of the method signature howcome it's giving me the error if i changed the modifier from protected(superclass) to private in the subclass.

Seems like a good question. Why should the compiler care one way or the other? If the methods are static there is no polymorphism involved in the method calls. It doesn't seem like there is a reason for that restriction. Can someone clarify this for me?!?!

Thanks,
Greg

Tony Alicea
sheriff
posted March 06, 2000 06:46 AM             
Just substitute "overridden" with "hidden" when dealing with static methods. Although static methods cannot be overridden in the strict (polymorphic) sense of the word, many people use the loose language anyway. I don't like it but what can I do but not use it myself

Signature and accessibility are two independent things with a method. That's all. Accessibility cannot be less in an overridden method, and the signature is the name of the method and the number, type and order of the args.

Ramana Namuduri
greenhorn
posted March 06, 2000 08:44 AM             
Thanks for correcting me. I used the word "overridden" instead of "hidden" in the case of static methods.
Thanks Maha Anna
Ramana

[This message has been edited by Ramana Namuduri (edited March 06, 2000).]

|