aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method in a subclass 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 "Static method in a subclass" Watch "Static method in a subclass" New topic
Author

Static method in a subclass

Kathy Hodgson
Greenhorn

Joined: Jun 16, 2003
Posts: 28
In the K and B book, page 103, there is a statement that static methods cannot be overridden. But in one of Dan Chisolm's practice exam answers, it is stated that "If the superclass method is static, a subclass method with the same signature must be static."
I thought that if a static method could not be overridden, I could redefine it in the subclass without any override restrictions (in other words just use the same name for a completely independent method with the same parameters and return type), but when I tried to compile an example, I found that I had to make the subclass method static to make it compile. Are there restrictions about redefining in a subclass that made that necessary, or was this really an override, and the K and B book was in error on that point?
Thanks,
Kathy
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Static methods are not overridden... they are hidden. Static methods do not participate in polymorphism so "hidden" is the correct term.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Hi Kathy,
As far as I know, you can't override a static method to a non-static one. So the restriction is that you have to keep the method with modifier static in your 'overriding'.
In fact, a static method doesn't override really the method of the superclass, but 'hides' it.


A static method may hide a method in the super class, but a static method does not override a super class method.

Overriding has to do with polymorphism and dynamic ligature at runtime, depending on the type of the object actually referenced at runtime, although
hiding deals with the type of reference.
More about this, if it can help:
http://www.coderanch.com/t/242443/java-programmer-SCJP/certification/inheritance
In fact, the same process of 'hiding' occurs both for instance variables and static methods. All are chosen at runtime, not based on the type of the object instance referenced at runtime, but on the type of the reference .
Hope this can help,
Cyril.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Kathy Hodgson
Greenhorn

Joined: Jun 16, 2003
Posts: 28
Thank you both; I think I understand now. Cyril, thanks for the link, that helped too.
Kathy
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Kathy
This I guess was said earlier by Thoam Paul, though I am not sure. The way you can also think about is that if you ever override a static method to be non-static then how would you invokle that method. That is let say there is a class A and it has static method named meth(). Now if class B which extends has non-static method meth() then when you use A's type variable which is pointing to a B instance which method would be invoked.
Here is what I am saying

Now b.meth() should invoke which method A's or B's. To avoid this kind of situation the overriding of static methods is prohibited.
Saravanakumar Rajmohan
Greenhorn

Joined: Jul 08, 2003
Posts: 17
Hi Anupam,
quote
--------------------------------------------
"A b=new B();b.meth();
Now b.meth() should invoke which method A's or B's. To avoid this kind of situation the overriding of static methods is prohibited. "
-----------------------------------------------
The above scenario does not pose any confusion
as u know there are 2 steps involved in method invocation
1. compiletime despatch
2. runtime despatch
during compile time it locates the class of the method, the method parameters ,its type ,its return type and also the invocation mode.
This is information is passed during runtime which aids in method dispatch.
the invocation mode for the above would be "static" since the method has a static modifier.
during runtime the first step is to calculate
the target reference. but if the invocation mode is static it does not calculate the target reference. it invokes the method based on the type.(located during compile time)
is there any other explanation ?
please explain?
thanks
Saravanakumar R
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Saravanakumar

the invocation mode for the above would be "static" since the method has a static modifier.

Well so won't be the case. I guess that this code will help you understand this more clearly.

Results in compilation error.
Now how would you know which method do I wish to call. Did I intented to call A's meth() or B's meth(). So this is why static method overriding is not allowed.
[ July 17, 2003: Message edited by: Anupam Sinha ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method in a subclass