• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritance of static methods

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Valentin. I just needed a little reassurance.
Corey
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the child static method just overrode the parent static method. Correct me if I'm wrong.
Thanks,
Jenny
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Super clear. Thanks, Corey!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic