1

I have a class that will be subclassed. All subclasses must contain a static method with the same signature, but differnt for each one.

I would like to have an abstract instance method in the superclass that subclasses will override, but it seems mot possible in Java, I wonder why.

A silly example:

  • Image{ abstract String getExtension();...

  • RGBImage extends Image{ static String getExtension(){return "RGB"};..

  • PNGImage extends Image{ static String getExtension(){return "PNG"};...

pepin
  • 11
  • 2
  • Duplicate :http://stackoverflow.com/questions/370962/why-cant-static-methods-be-abstract-in-java – sateesh Jan 12 '10 at 11:41

9 Answers9

1

Have a look at this explanation. You could use the Builder pattern for your purpose.

b_erb
  • 19,644
  • 8
  • 51
  • 63
1

As others have said, Java does not support overriding or abstract for static methods. However, I don't really understand what you would achieve with an "abstract" static method anyway.

The normal use of an abstract method is to force every subclass to implement a method with the same signature. But why would it even it matter for a static (hence non-polymorphic) method? If you forget to provide the method for one of the subclasses, EITHER it doesn't matter because you don't call it, OR it does matter but you'll get a compilation error at the point you try to call the missing method.

The only use-case I can think of where it might matter is if you are calling the method reflectively in a pseudo-polymorphic way. But if you are doing that you'd be better off using real polymorphism and instance methods.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
0

I don't do much Java, but (using C++ logic) you could make the base class implementation a dummy implementation that cannot be executed.

static void DoStuff(arg_type arg)
{
    std::cerr << "Method DoStuff() must be overriden\n";
    ::abort();
}

That isn't as good as an abstract method, but it will achieve some of the same results.

Michael J
  • 6,879
  • 1
  • 21
  • 28
0

Unfortunately, this is not possible in Java. You have to make them instance methods to be able to define as being abstract.

Erik Schierboom
  • 15,025
  • 10
  • 60
  • 79
0

You want to override instance method with static method ? That makes no sense. Static methods are not polymorphic. How would you like to call it? It is impossible in Java to enforce class to have a static method with given signature.

Piotr Gwiazda
  • 11,796
  • 12
  • 53
  • 87
0

It's not possible to override static methods as they pertain to a class, not an instance.

Furthermore, an instance method cannot be made static in the sub-classes as you suggest in your example.

Can you provide more information as to why all the subclasses need to have the same static method?

fridrikr
  • 48
  • 3
0

The reason I want to have the abstract method declared in the superclass is because the class has to do some extra work with the result of the abstract method.

Lets put it simple even the example has no sense:

Image{

abstract String getExtension();

int process(){

   return getExtension().length

}

The algorithm in process() is common for all subclasses XXXImage of Image, so its right place is the superclass.

Let me say, everything works OK implemnting a instance getExtension() method in each subclass. But the point is that the method is in fact static in the subclass, has no dependence on the instance.

I know I can't do that, I only saying it seems no so strange idea to me.

pepin
  • 23
  • 2
0

as others have said, static is not polymorphic... it means it is the same for the class, not specific to an instance. i am not sure what you would accomplish by using a static method... but you could have have your extended method implementation return a static value:

public String getExtension() {
  return STATIC_CONSTANT;
}

I guess it depends on what you are trying to accomplish with the static method. From your post: "Let me say, everything works OK implemnting a instance getExtension() method in each subclass. But the point is that the method is in fact static in the subclass, has no dependence on the instance. I know I can't do that, I only saying it seems no so strange idea to me."

The problem is you are asking for polymorphic behavior (a change in the behavior of a method for each subclass) from something that defines behavior for the class, not the instance. By definition, static cannot be polymorphic. I hope this helps you with your understanding here of why this is not possible.

PaulP1975
  • 488
  • 1
  • 5
  • 12
-2

Yes thaks, this is what I'm doing, retun a static constat. In may case it returns the class object of a inner enum that lists the name of sections of a given file format: the file format of the subclass.

But you put it very clear in your example:

public String getExtension() {

return STATIC_CONSTANT;

}

I was expecting to be able to put "static" before String...

pepin
  • 23
  • 2