Re: abstract static methods (again)
posting-account=pAbxHwoAAAADCfRr12ntKYVMSzbDntdj
Tomas Mikula wrote:
On Wed, 21 Oct 2009 06:10:02 +0100, Steven Simpson wrote:
In fact, since you expect (from the previous question) that any Y which
extends X must provide its own static run(), I don't see a point in
requiring X to 'implements static Runnable' if it's only going to leave
the implementation to a base class.
The point would be to enforce the subclasses to 'implement static'
Runnable. Consider again an abstract class Vector with static method
zero(), which should return a zero vector. Vector alone doesn't need
(even can't reasonably) implement zero(), but it wants to enforce its
implementation in all concrete subclasses.
I anticipated shortly after posting that your Vector example might be
what you were getting at. Here's how I think it would go (assuming
reified generics). Your original declaration:
class MyClass<V extends Vector<V>> {
public void someMethod(){
V v = V.zero();
...
}
}
So, on some class whose full implementation you don't know about, you
expect a (V zero()) method - so define an interface type for that:
interface Zero<V> {
V zero();
}
Now express two constraints on the type provided to MyClass:
class MyClass<V extends Vector<V> & V extends static Zero<V>> {
public void someMethod(){
V v = V.zero();
...
}
}
Does that work for you?
If so, there's no need to put an 'abstract static' on Vector. It's only
MyClass that imposes the requirement. Other classes analogous to
MyClass might not need to call zero(), and could use extensions of
Vector that don't also 'implement static Zero' - such extensions could
not exist if Vector imposed the requirement on all its descendants.
--
ss at comp dot lancs dot ac dot uk