Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java abstract static Workaround

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.

like image 849
twolfe18 Avatar asked Dec 16 '09 16:12

twolfe18


People also ask

Can we override static abstract method in Java?

Declaring abstract method static If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.

Can we use static and abstract together in Java?

A static method belongs to class not to object instance thus it cannot be overridden or implemented in a child class. So there is no use of making a static method as abstract.

Can abstract method have static method?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.

Can abstract class have static variables in Java?

Type of variables: Abstract class can have final, non-final, static and non-static variables.


2 Answers

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.

like image 197
Dave Avatar answered Sep 20 '22 13:09

Dave


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.

like image 39
Roman Avatar answered Sep 19 '22 13:09

Roman