• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

need help!!

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks joe & monty.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou ramana.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic