views:

299

answers:

12

I understand that neither a abstract class nor an interface can contain a method that is both abstract and static because of ambiguity problems, but is there a workaround?

I want to have either an abstract class or an interface that mandates the inclusion of a static method in all of the classes that extend/implement this class/interface. Is there a way to do this in Java? If not, this may be my final straw with Java...

EDIT 1: The context of this problem is that I have a bunch of classes, call them Stick, Ball, and Toy for now, that have a bunch of entries in a database. I want to create a superclass/interface called Fetchable that requires a static method getFetchables() in each of the classes below it. The reason the methods in Stick, Ball, and Toy have to be static is because they will be talking to a database to retrieve all of the entries in the database for each class.

EDIT 2: To those who say you cannot do this in any language, that is not true. You can certainly do this in Ruby where class methods are inherited. This is not a case of someone not getting OO, this is a case of missing functionality in the Java language. You can try to argue that you should never need to inherit static (class) methods, but that is utterly wrong and I will ignore any answers that make such points.

A: 

No. You can't do that. If you're willing to compromise and make the method non-static or provide an implementation of the static method in your abstract class, you'll be able to code this in Java.

Asaph
A: 

It doesn't make sense to do what you're asking:

http://stackoverflow.com/questions/370962/why-cant-static-methods-be-abstract-in-java/370966#370966

GaryF
how does it not make sense to do what i'm asking. if you had a couple classes that all needed a static method getAllWidgets (where widget is the superclass or interface), why is it unreasonable to want to require a static method (in each of the subclasses) getAllWidgets that returns a collection of instances of the subclass/class that implements the interface?
twolfe18
-1 for not reading my question.
twolfe18
+4  A: 

Is there a way to do this in Java?

I don't think there is a way to do this in any language. There's no point to it, since static methods belong to a class and can't be called polymorphically. And enabling polymorphic calls is the only reason for interfaces and abstract classes to exist.

Michael Borgwardt
so in each of the subclasses i just need to write a static method getAllWidgets, there's no way to indicate programmatically that that method exists?
twolfe18
How would you call it? besides, there's probably a better solution: have that method fully implemented in a base class, taking a Class object as parameter - the base class constructor can take cake or registering all instances.
Michael Borgwardt
@Michael what you describes is similar to what I ended up doing. but to your first question, say class A extended this abstract class which required abstract static getBlahBlah(), you would call it like A.getBlahBlah(). I don't really see why people are taking this so poorly, I don't think I'm asking something unreasonable.
twolfe18
@Michael, also, I know that in my previous comment that you can't have an abstract static class, and that if you called (A's super class).getBlahBlah() there would be a ambiguity problem, but my original question was about a workaround to this problem.
twolfe18
The thing is: interfaces and abstract classes are part of the language's formal type system. The methods you need aren't - they're a convention for the convenience of the programmer.
Michael Borgwardt
@Michael I see your point, but I don't believe the language's design (type system or otherwise) should be limiting or more important that what the programmer wants to do (provided the programmer isn't an idiot... which I don't think I am...). I guess its just a philosophical difference between me and the people who wrote Java. I think this is a case of poor design in Java, but maybe I'm just getting too comfortable with duck-typed languages.
twolfe18
Java isn't limiting here - quite the opposite: it doesn't enable *you* to force programmers using your class / implementing your interface to follow your convention. How would you do the thing you're after in a duck-typed language? Don't those provide even fewer ways to do things like that?
Michael Borgwardt
@Michael - Java is limiting: by my book, my will trumps Java's ideas about what I should and shouldn't force other programmers to do. I may have mis-spoke about the solution being easier in a duck-typed language. What I meant is that in a language like Ruby, a class is an object, so it makes sense to inherit static methods. This means that I could at least do what I want to do (if Ruby had interfaces/abstract classes...), but I might get a NotImplementedError. Still ugly. I'm sure there is a language out there that has an elegant way to do what I want to do!
twolfe18
@twolfe18 - "I'm sure there is a language out there that has an elegant way to do what I want to do!" Well stop complaining about Java, and start looking for that language. :-)
Stephen C
@Stephen, this language is Object Pascal (or Delphi if you prefer) and this way is called "virtual class methods", see http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devwin32/classmethods_xml.html#code00526
mschayna
@mschayna - tell it to @twolfe18 ... I'm not the one complaining about Java :-)
Stephen C
A: 

Create a context interface containing your method with a name that matches your problem domain. (Name it "World" if you absolutely have to, but most of the time there's a better name)

Pass around implementation instances of the context object.

fforw
kind of ugly, but it could work. i probably wouldn't use this though because its uglier than my current solution which is to just have a version of that static method in each of my subclasses.
twolfe18
A: 

Ok, maybe my question was poorly asked, it seems like most of you didn't get what I was trying to do. Nonetheless, I have a solution that is somewhat satisfactory.

In the abstract super class, I am going to have a static method getAllWidgets(Class type). In it I'll check the class you passed it and do the correct fetching based on that. Generally I like to avoid passing around classes and using switches on stuff like this, but I'll make an exception here.

twolfe18
It's a very common case of badly-asked question: you had already decided on an (unworkable) solution to your actual problem and then asked how to get that solution to work without mentioning your actual motivation. BTW, you shouldn't have to use a switch here, a Map<Class, List<Widget>> should do the trick. Should even be possible to use a generic type parameter so that the method becomes typesafe.
Michael Borgwardt
You don't really seem to get OO (as most answers here have pointed out). I'll give you one hint, if you are relying on static methods for more than trivial functions, you're doing it wrong. If you posted more of your problem we might be able to help you but as with most programming problems, it's hard to solve correctly without a much bigger picture. I would suggest you redo your design with no statics at all to break yourself of the habit for starters. I wish there was an easy way to share UML style design diagrams--seeing yours might help too.
Bill K
@Michael - your recommendations about the map and the type parameter are noted, good points. I did not however have this solution planned when I asked the question.
twolfe18
@Bill - Frankly I don't appreciate you telling me I don't understand OO. I am working on an application that is requires dealing with collections of objects a lot, so methods like getAllWidgets are needed (I consider them trivial), and they need to be static. Maybe you should spend less time on SO and more time with a framework like Rails to get a better idea of how OO can be applied more broadly.
twolfe18
@twolfe: your "trivial" methods aren't trivial, because of the problems you're running into here. If you need polymorphism, they cannot (and should not) be static methods.
Andrzej Doyle
@Andrzej - 1. the methods are trivial, working them into Java seems not so trivial. 2. do you really think that a method that fetches a bunch of objects irrespective of the context should not be static? 3. what is wrong with inheritance of static methods? it seems like everyone here is arguing: "Java doesn't let you do it, so it must be wrong!" Anyone have any real reasons why you shouldn't have polymorphism and static methods together?
twolfe18
@twolfe18 I've spent a year working on rails, 10 years programming in OO, and 20 years programming altogether. (I do like Active Record, BUT it's not perfect). You shouldn't be passing around naked collections at all, you should be passing around business objects. Does your class diagram actually have a bunch of collections in it? If you didn't bristle and actually tried what I suggested (avoiding static methods) you might learn something sooner than later--but if you want to learn your own way, enjoy (most of us learned by doing it wrong the first time--nothing wrong with that!)
Bill K
@Bill - how do you propose I do this without static methods? the only way I can think of is to create a singleton called WidgetCollection. singletons are still just a OO workaround for static methods (I think singletons are not very OO at all -- still valuable though). but two more things: 1. I don't necessarily think that OO is the second coming of Christ, there is more than one way to write an elegant program. 2. if you're not proposing I use singletons, do you care to explain how you would do it?
twolfe18
I think until an actual example of what you are really doing (and not just interfaces) is provided then this argument will continue to go around in circles. There is a disconnect here somewhere and the "crowd" is not properly equipped to bridge the gap. Maybe you can edit your question with a more concrete use-case?
PSpeed
@Bill, if you have worked with rails, you must see the similarity between what I want to do and ActiveRecord's find method? find is a static method that is inherited by all methods that extend ActiveRecord::Base, thats what I want to do in Java.
twolfe18
@twolfe18 You seem to need different WidgetCollections instances, not a singleton (otherwise you wouldn't be asking about different static methods)--but as I said, I don't have a big enough picture to feel sure about that. Also a "WidgetCollection" sounds generic, not really like a business class. Usually such an object is attached to or created from another object, so you rarely need things like static factory methods either (usually these are another indication that your OO could be better). I understand not liking OO, but if you're going to do it then don't go at it half assed.
Bill K
I understand what you are saying, but Ruby is a very different language from Java. You can't implement AR in java--and I wouldn't even suggest trying. The big difference is that Ruby can bind your data with your code on it's own (This is why I was impressed with AR I've tried to do it in Java and it's HARD). You might look into Hibernate for our best approximation.
Bill K
+1  A: 

There are lots of answers about 'this does'nt make sense..' but indeed I met a similar problem just yesterday.

I wanted to use inheritance with my unit tests. I have an API and several its implementations. So I need only 1 set of unit tests for all implementations but with different setUp methods which are static.

Workaround: all tests are abstract classes, with some static fields with protected access modifier. In all implementations I added static methods which set these static fields. It works rather nice, and I avoided copy and paste.

Roman
this is pretty much equivalent to what I am going to do, with some tweaks. I need to worry about the return type for the static methods though.
twolfe18
A: 

static methods can't be abstract because they aren't virtual. Therefore anywhere that calls them has to have the concrete type with the implementation. If you want to enforce that all implementations of an interface have a certain static method, then that suggests a unit test is required.

abstract class A
{
    public static void foo()
    {
        java.lang.System.out.println("A::foo");
    }

    public void bar()
    {

        java.lang.System.out.println("A::bar");
    }
}

class B extends A
{
    public static void foo()
    {
        java.lang.System.out.println("B::foo");
    }

    public void bar()
    {

        java.lang.System.out.println("B::bar");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        B b = new B();
        b.foo();
        b.bar();

        A a = b;
        a.foo();
        a.bar();
    }
}
Douglas Leeder
A: 

You have a couple of options:

  1. Use reflection to see if the method exists and then call it.
  2. Create an annotation for the static method named something like @GetAllWidgetsMethod.

  3. As others have said, try to not use a static method.

Dave
can you explain how the annotation solution would work (2)?
twolfe18
Basically, you could look at the class's static methods and see if one has the annotation and then call it. Neither option 1 or 2 provide a way of enforcing that at compile time, however.
Dave
A: 

A type system allows you to express some constraints among types, but it's limited. That's why javadocs are littered with constraints in human language, asking people to follow rules that the compiler cannot check.

if you want to extend it beyond what language provides natively, you can write your own static analysis tool. that is not uncommon. for example: findbug. also IDEs do that too, they checking thing beyond what language dictates. you can write a plug in to enforce that a subclass must have a static method of such signature.

in your case, it's not worth it. have javadoc in the superclass urge implementors to include a static method, that's good enough.

I'll provide a convoluted way of expressing your constraint anyway, but DO NO DO IT. people get really carried away of make everything checkable at compile time, at the price of making code unreadable.

interface WidgetEnumerator
{
    List getAllWidgets();
}

public class Abs<T extends WidgetEnumerator>
{
    static List getAllWidgets(Class<? extends Abs> clazz){ ... }
}

public class Sub extends Abs<SubWidgetEnumerator>
{
}

public class SubWidgetEnumerator implements WidgetEnumerator
{
    public List getAllWidgets() { ... }
}

How it works: for any subclass of Abs, it is forced to provide an implementation of WidgetEnumerator. subclass author cannot forget that. Now invocation Abs.getAllWidgets(Sub.class) contains sufficient information to resolve that implementation, i.e. SubWidgetEnumerator. It is done through reflection, but it is type safe, there are no string literals involved.

irreputable
A: 

I'd make a WidgetCollection class with an abstract Widget inner class.

You can extend the WidgetCollection.Widget class for each of your types of Widget.

No static methods necessary.

Example (not compiled or tested):

class WidgetCollection<W extends Widget> {
    Set<W> widgets = new HashSet<W>();

    Set<W> getAll() {
        return widgets;
    }

    abstract class Widget {

       Widget() {
           widgets.add(this);
       }

       abstract String getName();
    }

    public static void main(String[] args) {
         WidgetCollection<AWidget> aWidgets = new WidgetCollection<AWidget>();
         a.new AWidget();

         Set<AWidget> widgets = aWidgets.getAll();
    }
}

class AWidget extends Widget {
    String getName() {
        return "AWidget";
    }
}
Skip Head
A: 

I think I can give you a better answer after seeing your edits--your best bet is probably a factory pattern. (Not lovely, but better than singleton).

abstract class Widget
    public static Widget[] getAllWidgetsOfType(Class widgetType) {
        if(widgetType instanceof ...)
    }
class Ball extends Widget
class Stick extends Widget
class Toy extends Widget

This is not a very good way to do it, but it's typical. Hibernate is the tool you would normally use to solve this problem, this is exactly what it's designed for.

The big problem is that it requires editing the base class whenever you add a new class of a given type. This can't be gotten around without reflection. If you want to use reflection, then you can implement it this way (Psuedocode, I'm not going to look up the exact syntax for the reflection, but it's not much more complex than this):

public static Widget[] getAllWidgetsOfType(Class widgetType) {
    Method staticMethod=widgetType.getStaticMethod("getAllInstances");
    return staticMethod.invoke();
}

This would give the solution you were asking for (to be bothered by the need to modify the base class each time you add a child class is a good instinct).

You could also make it an instance method instead of a static. It's not necessary, but you could then prototype the method (abstract) in Widget.

Again, all this is unnecessary and sloppy compared to Hibernate...

Edit: If you passed in a live "Empty" instance of a ball, stick or toy instead of it's "Class" object, you could then just call an inherited method and not use reflection at all. This would also work but you have to expand the definition of a Widget to include an "Empty" instance used as a key.

Bill K
A: 

Static methods are relevant to an entire class of object, not the individual instances. Allowing a static method to be overridden breaks this dictum.

The first thing I would consider is to access your database from a non-static context. This is actually the norm for Java apps.

If you absolutely must use a static method, then have it parameterised with instance specific arguments (of a generic type) to allow the different subclasses to interact with it. Then call that single static method from you polymorphic methods.

Cogsy