2

What it says on the tin. Apologies and links appreciated if this has been asked before, just I could not find anything after some digging.

Given that interfaces don't allow keywords like static, and "abstract static" is not a thing in Java (yet) (see the discussion here, Why can't static methods be abstract in Java), do I have ANY way to say "every descendant of Foo has a guaranteed static method Bar" ?

Community
  • 1
  • 1
Jessica Pennell
  • 578
  • 4
  • 14
  • 4
    Short answer: no. Long answer: noooooooooooooooo. – Louis Wasserman Sep 09 '15 at 19:04
  • 1
    related, explains why [even if you had this it wouldn't be useful](http://stackoverflow.com/a/31593727/217324) – Nathan Hughes Sep 09 '15 at 19:25
  • Hi Nathan, I read that article, but it wasn't a very satisfying explanation; it would be trivially easy to use Java's generics to work around the get-at-data-in-the-subclass problem, and especially since we can't prescribe constructors, the comment "With OO the idea is you can avoid being specific and let the subclasses take care of themselves" in the page you linked to seems particularly ironic if you want a generic factory method. – Jessica Pennell Sep 09 '15 at 19:35
  • @NathanHughes still very much appreciated, thanks for tracking that down and helping me understand some of the thinking involved. – Jessica Pennell Sep 09 '15 at 19:38
  • All I went ahead and accepted @TAsk 's answer because I have to head back but thank you very much for the enlightening discussion – Jessica Pennell Sep 09 '15 at 19:44

3 Answers3

3

every descendant of Foo has a guaranteed static method Bar

Not really, In case of default methods you can only override the default methods if used in child other than that every child will have common implementation in interface. Moreover, default methods are not static.

On the other hand, static methods are allowed in Java-8 but you can directly access it with interface name and it does not mean only children of interface will have that static method. But this static method will be accessible for every class which can import or reach to the interface.

akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    Moreover, `static` `interface` methods are not inherited by the implementing classes, so regarding these methods there is no difference between implementors and arbitrary other classes at all. – Holger Sep 10 '15 at 09:40
1

Most OO languages accept normall non-static virtual methotds. Some, maybe niche, i.e. like Delphi has idea virtual static methods.

In derived object can call static method, and is used adequate to object class. Many haven't this concept.

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22
1

Your question is a non-issue in the java language. You cannot 'prescribe' subclasses to have a static method x(), and even if you could, there would be no language element allowing you to actually call the subclass static method (except by qualifying it with the child class literal, which defeats the purpose).

This looks like an XY question. Better ask about the problem you intend to solve.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • Hi Durandal, an example situation where this might be useful : `abstract class A {abstract static A configuredNewInstanceFactoryMethod();}` – Jessica Pennell Sep 09 '15 at 19:27
  • @JessicaPennell Thats commonly solved using the Builder/Factory design patterns. There's also the reflection API, if you want to use normal constructors and no extra classes; although thats obviously not compile time safe. Both can in practice often be avoided by moving object creation to the place where the type information is known, e.g. instead "Y createFrom(X)", use "X.createY()". You will rarely need to select a subclass out of thin air, in many cases there is already an object that can make the decision. – Durandal Sep 09 '15 at 19:39
  • I posted before I saw your most recent comment – Jessica Pennell Sep 09 '15 at 19:40