By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

another design question, abstract static methods...

P: n/a
so here is another general question about java...

why can't you declare an abstract static method.

i can envision the case (indeed i have experienced the case) where one
would want an abstracct superclass, with an abstract method such that all
subclasses that implement this method make that method static.

basically, the abstract class declares an abstract method that should be
static for all implementations (for example a factory method).

this isn't allowed in java, and in the language specification, there is no
reason given as to why. it is just simply stated in a single sentence
that an abstract method cannot be static. (or it turns out even the
implemented method).

can anyone explain why this isn't allowed? (i.e. give an example that
shows the flaws that can arise from such a situation).

thanks much!

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Polymorphism in Java is resolved by the type of the actual object (dynamic,
runtime) the method is called against instead of the class of the reference
(static, compile time). With static methods there is no object in play, only
a class so it can only ve resolved statically during compiletime.

What calling constructs (and behaviour) do you seek for?

Silvio Bierman
Jul 17 '05 #2

P: n/a
Murat Tasan wrote:
so here is another general question about java...

why can't you declare an abstract static method.

i can envision the case (indeed i have experienced the case) where one
would want an abstracct superclass, with an abstract method such that all
subclasses that implement this method make that method static.

basically, the abstract class declares an abstract method that should be
static for all implementations (for example a factory method).


I do not understand what you expect to accomplish this such a method.
How do you envision it being used? Perhaps if you provide a code
sample, I could understand.

From my point of view, I do not understand the point of a static
abstract method. Static method calls are determined at compile time,
not at run time, so it is not possible for the invocation to be
polymorphic. How would one invoke a static method so that one would
expect polymorphism in any case? If you have an instance of the class,
then the method might as well be non-static. If you are using
reflection, all that matters is that the method is defined in the
current class, the base class does not matter.

Furthermore, a abstract class is allowed to have abstract instance
method because it is not possible to instantiate an instance of the
class. But one may invoke static methods of an abstract class without
an instance. So there is no mechanism to ensure that an abstract static
method is not called.

Of course, many of the points I made above are really design decisions
of the Java language designers. So I am very interested in hearing how
you response to them to make the case for the other side.

Ray

Jul 17 '05 #3

P: 25
Here's an example where one would like to use an abstract static method -- or better yet, an interface with the static method! -- neither of which is allowed:

Expand|Select|Wrap|Line Numbers
  1. public abstract class GenericWidgetFactory {
  2.   public static abstract Widget createWidget(Object params);
  3.   }
  4.  
  5. public class BigWidgetFactory extends GenericWidgetFactory {
  6.   public static Widget createWidget(Object params) {
  7.     // code here to create big widgets
  8.     }
  9.   }
  10.  
  11. public class SmallWidgetFactory extends GenericWidgetFactory {
  12.   public static Widget createWidget(Object params) {
  13.     // code here to create small widgets
  14.     }
  15.   }
Expand|Select|Wrap|Line Numbers
  1. public interface GenericWidgetFactory {
  2.   public static Widget createWidget(Object params);
  3.   }
  4.  
  5. public class BigWidgetFactory implements GenericWidgetFactory {
  6.   public static Widget createWidget(Object params) {
  7.     // code here to create big widgets
  8.     }
  9.   }
  10.  
  11. public class SmallWidgetFactory implements GenericWidgetFactory {
  12.   public static Widget createWidget(Object params) {
  13.     // code here to create small widgets
  14.     }
  15.   }
This construct is very useful because it allows the compiler to enforce the syntactical structure of the inherited classes (Big and SmallWidgetFactory).

Any clever ideas around this without instantiating an object?
Thanks,
Jerry
Jul 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.