aspose file tools *
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance of static methods 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 "Inheritance of static methods" Watch "Inheritance of static methods" New topic
Author

Inheritance of static methods

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Okay, I've written some code to see what happens when you inherit from a class that has a static method and I just want to ensure that I'm interpreting the results correctly.
These are the classes that I used:

Here are a few things that I've found. It does appear that static methods are inheirted. I found this by removing the declaration of staticMethod() within Child. When that was removed, the code within main still ran fine and I received the output "Parent's static method." Also, I found that if I try to make the declaration of staticMethod() within Child more restrictive than the staticMethod() in Parent, I get a compiler error stating: "Cannot reduce the visibility of the inherited method from Parent." So, looking at these things, it would appear as if static methods are inherited just like any instance methods.
However, I have found that static methods do not use dynamic method lookup in order to determine which one should be executed. In the example I gave above, the output from main is: "child's static method." However, if I change the type of c in main from Child to Parent, the output I get is: "Parent's static method." Therefore, I can conclude that the method that is called is based upon the compile-time type of the variable on which it is being called.
This area is just a little bit foggy for me and I just wanted someone to take a look at my conclusions to ensure that they are correct. From what I've tested, they appear to be, I just want a second opinion.
Thanks,
Corey
P.S.
How can I get spaces between lines within a code block? Any time I try to add blank lines in a code block, it seems to be ignored and all of my lines are jammed together.


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey you got it perfectly right
To sum up,
- static methods are inherited
- the accessibility of static methods in the subclass cannot be made more restrictive than the hidden static method in the superclass
- no dynamic method lookup. invocation type is based upon the compile-time type, that is, the name of the class standing on the left of the reference variable:
Parent c = new Child();
Parent is the compile-time type of c
Child is the runtime type of c
HIH
[ February 12, 2002: Message edited by: Valentin Crettaz ]

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

Joined: Dec 20, 2001
Posts: 3271
Thanks, Valentin. I just needed a little reassurance.
Corey
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Another way to put it is that although static methods are inherited, they cannot be overriden like instance methods; they can only hide declarations of the same signature in the superclass.


Rob
SCJP 1.4
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
I thought the child static method just overrode the parent static method. Correct me if I'm wrong.
Thanks,
Jenny
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jenny Yin:
I thought the child static method just overrode the parent static method. Correct me if I'm wrong.
Thanks,
Jenny

No, the method in the class Child did not override the method in the class Parent. Overriding methods implies that dynamic lookup will be used at runtime to determine which method to call based on the runtime type of the object the method was called on. When using static methods, this is not the case. Rather, the method corresponding to the compile time type of object you call the method on will be executed. The method is not overridden, it is simply hidden (if you can even call it hiding).
Try overriding a static method and then calling the parent class' method by using the keyword super, like this:

Try running this code. You should find that it doesn't even comile. That's because you can't use the keyword super in a static context (nor can you use this, for that matter), because the object might not be instantiated. Therefore, how can you override a method when the parent object might not even exist.
You can, however, call the doIt() method in Parent using line 2.
Does that help?
Corey
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
I think I stated it wrong in the last post. Overriding is a runtime binding which doesn't happen to static methods, right?
Thanks,
Jenny
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Super clear. Thanks, Corey!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jenny Yin:
I think I stated it wrong in the last post. Overriding is a runtime binding which doesn't happen to static methods, right?
Thanks,
Jenny

Correct. If you go all the way back to the original example, you can change the final output by changing the declared type of c from Child to Parent. The method called reflects this declared type, not the run-time type.
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance of static methods