0

This is a strange question but I was wondering if there was a way to "override" a parent class's static method in a subclass and call that subclass's static method from the parent class.

It would look something like this

public class parentFoo {
    public static void <T extends parentFoo> printFoo () {
        T.printFoo();
    }
}

public class childFoo extends parentFoo {
    public static void printFoo() {
        System.out.println("Foo");
    }
}

so you could do something like this in your code

 //print out "Foo"
 parentFoo.<childFoo>printFoo();

This isn't working for me but I was wondering if there is some way to make this possible. Right now I get a stack overflow because it only calls the parent class's printFoo method.

ScottShamus
  • 415
  • 5
  • 6

3 Answers3

2

You cannot override static methods. You can however define static methods of the same name. If you do that then you can specify which one is called from the class type

ChildFoo.printFoo(); // call child foo
ParentFoo.printFoo(); // call parent foo

ParentFoo foo1 = new ChildFoo();
foo1.printFoo(); // ParentFoo still called because of type of reference foo1 not its value

ChildFoo foo2 = new ChildFoo();
foo2.printFoo(); // ChildFoo called because of type of reference foo2 not its value
Adam
  • 35,919
  • 9
  • 100
  • 137
  • Yep, you are correct. But I was just wondering if there was some way to use generics to point towards the subclass's static method. – ScottShamus Mar 12 '12 at 20:59
0

There's no way to to call a static method based on a generic type parameter.

The answer to the question below provides a reasonable workaround.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Yep, you are correct. But I was wondering if there was some way use generics to use a subclass's static method with the same name. I shouldn't have used the word "overridden" in my question, that was a bad choice. – ScottShamus Mar 12 '12 at 20:56
  • Ah, ok. I guess that's what I was actually trying to ask. Thanks for the link. – ScottShamus Mar 12 '12 at 21:01
  • @aioobe can I please check with you for a theory along these lines? There's a difference between instantiating and sub classing. From what I learnt a static class can't be instantiated - it is fair. But can a static class be sub classed, although it doesnt really make sense to me to sub class a static class..unless we can only use few unrestricted static methods in static parent class and we need to add more new/our app specific static methods into a separate static child class. Ya in brief can we sub class a static class? :-) – aspiring Feb 12 '13 at 11:28
  • *There's a difference between instantiating and sub classing* -- Right. Those are two different concepts. Instantiation is runtime, subclassing is compile time. *From what I learnt a static class can't be instantiated* -- Sure it can. *But can a static class be sub classed* -- Absolutely. – aioobe Feb 12 '13 at 11:35
0

Replace

parentFoo.<childFoo>printFoo();

with

childFoo.printFoo();

Why would you need to override static methods? (which isn't possible) What are you trying to accomplish?

A polymorphic call only makes sense for objects. Since statics aren't part of an object, but of a class, it doesn't.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • This is more to satisfy my own curiosity with Java generics. I'm not really sure what it would be used for but I was wondering if it would be possible. I was just wondering if you could use generics to indicate which static method could be used. – ScottShamus Mar 12 '12 at 20:54
  • @ScottShamus I can see how you would do that in C++, but not for Java. – Luchian Grigore Mar 12 '12 at 20:55