• Post Reply Bookmark Topic Watch Topic
  • New Topic

static inheritance  RSS feed

 
Paul John Summers
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would look this up in a book but I don't think I'll quite find the answer I'm looking for. My question is about overriding a static method. My base class is the following...



Now here is my child class. It only overrides the createFactory method




Unfortunately, when I try to use MyChildUtitlityClass, it still calls MyUtilityClass.createFactory().

So my question I guess is can a child class override the parent class' static method in this situation. I'm not a newbie but I don't often use static constructs such as these. Any advice would be greatly appreciated.

Thanks!
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this suitation, when we requesting a subclass, first superclass will be loaded first. At the time of calling static initializer of superclass, only superclass static method only available.

Regards
Kasimurugan R.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although what Mr. Ramasamy says is true, there's a more important and more fundamental principle at work here: static methods are not polymorphic. Despite what you may believe you've done here, you have not overridden the createFactory() method, you've "hidden" it.

The important point is that calls to static methods are bound at compile time, not at runtime. When the static initializer for MyUtilityClass is compiled, the call to MyUtilityClass.createFactory() is "hard-coded" into it: nothing will change the fact that exactly that method is going to be called whenever that initializer block is run.

Only instance methods -- i.e., non-static, non-constructor methods -- display polymorphic behavior.
 
Paul John Summers
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you for your responses to my question. While it creates a small challenge for me, I've definitely learned something fundamental about Java. Thanks!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!