-1

Update: I know that a static method can't be abstract, and I know why, and it's not a question like 'Whether it's possible?' or 'why it's not possible?', I just want to know if there's any way to get what I want.

Well, if you heard about @classmethod in python, you can skip the rest.

And here's what I want: In some cases, some methods are just functions, what I only need is its functionality. However, in Java, you have to put everything in a class, so I have to wrap these function in a class and set them static. The thing is, for classes like this one, I don't, and I shouldn't need any of its instance. And in the mean time, I want some hierarchy for classes like this. The hierarchy here works like some 'control': I can control sub-classes for what you should do, aka what method/function you should have.


As we all known, we can't declare a static abstract method in a class.

However, there can be situations when you only need the method rather than an instance, and you need the method to be abstract and implement it in sub-classes.

Some guys pointed out that this may be a poor design in java, but is there any possible solution for this?

6
  • What do you want to use it for? Commented Sep 30, 2014 at 8:09
  • 5
    No. It makes no sense to mark a static method as abstract. overriding of methods is based on the object (instance), not on the type itself. static methods are dependent only on the type and not on any particular instance. Why not directly call MyabstractClass.someMethod()? Commented Sep 30, 2014 at 8:09
  • You can't create an instance of an abstract class now, so I am now sure what you expect adding static would do differently, or what you are trying to do differently. Commented Sep 30, 2014 at 8:16
  • BTW If you only want abstract methods, an interface is likely to be a better choice. Commented Sep 30, 2014 at 8:17
  • This seems like an XY problem to me. What are you trying to do actually? Commented Sep 30, 2014 at 8:19

3 Answers 3

2

No, you can't
Mainly because the terms of 'abstract' and 'static' method contradict each other. See detailed explanantion here: Why can't static methods be abstract in Java

Sign up to request clarification or add additional context in comments.

Comments

1

your static method cant be abstract but you could use functional interfaces, the predefined ones like Function, BiFunction or even Consumer will most likely suit all your needs, heres a list of the predefined interfaces : http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

If these dont fit into your requirements you can write your own interfaces and hence be able to pass arbitrary method-references around ... be sure to always check for null, though

Comments

0

Static methods cannot be declared abstract, because overriding (subtype polymorphism) does not apply to static methods.If you need a workaround here's one:

public void myMainFunction() { ArrayList<Animal> animals = new ArrayList<Animal>(); animals.add(AnimalFactory.createAnimal(Bird.class,birdXML)); animals.add(AnimalFactory.createAnimal(Dog.class,dogXML)); } public abstract class Animal { /** * Every animal subclass must be able to be created from XML when required * (E.g. if there is a tag <bird></bird>, bird would call its 'createFromXML' method */ public abstract Animal createFromXML(String XML); } public class Bird extends Animal { @Override public Bird createFromXML(String XML) { // Implementation of how a bird is created with XML } } public class Dog extends Animal { @Override public Dog createFromXML(String XML) { // Implementation of how a dog is created with XML } } public class AnimalFactory{ public static <T extends Animal> Animal createAnimal(Class<T> animalClass, String xml) { // Here check class and create instance appropriately and call createFromXml // and return the cat or dog } } 

I have got it from here: Static abstract method workaround

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.