wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need help!!" Watch "need help!!" New topic
Author

need help!!

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 01, 2000
Posts: 8
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks joe & monty.
Ramana Namuduri
Greenhorn

Joined: Mar 02, 2000
Posts: 16
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).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thankyou ramana.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Feb 25, 2000
Posts: 17
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
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Ramana Namuduri
Greenhorn

Joined: Mar 02, 2000
Posts: 16
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help!!