I would like to force all subclasses of a given superclass to
implement a method public void update(...);
but the input may be arbitrary different, for example one class may
need the method update(int i); another one update(double a1, double
a2); and yet another one update(double[] abc);
Is there a way to do this? It is not really congruent with the whole
superclass concept, so maybe not...
thanks for your help.
Wojtek - 09 Dec 2007 21:28 GMT
c.scherr wrote :
> I would like to force all subclasses of a given superclass to
> implement a method public void update(...);
[quoted text clipped - 5 lines]
> Is there a way to do this? It is not really congruent with the whole
> superclass concept, so maybe not...
Make a class which holds these primitives, and use that as the
parameter. The sub-class can then use whatever it wants to use.
Or you can make a series of sub-classes which are also abstract each of
which have the specific parameter type.

Signature
Wojtek :-)
c.scherr - 09 Dec 2007 21:43 GMT
> c.scherr wrote :
>
[quoted text clipped - 16 lines]
> --
> Wojtek :-)
Alright. Thanks.
Mark Rafn - 09 Dec 2007 21:45 GMT
>I would like to force all subclasses of a given superclass to
>implement a method public void update(...);
>but the input may be arbitrary different, for example one class may
>need the method update(int i); another one update(double a1, double
>a2); and yet another one update(double[] abc);
Umm, doesn't that break the interface? Say some user has
ParentClass myObj = new SubClass();
myObj.update(12);
What should happen?
If update is a method that all subclasses must have, it must have the same
signature as it could be called by someone who doesn't know at compile-time
what the specific subclass is.
>Is there a way to do this? It is not really congruent with the whole
>superclass concept, so maybe not...
I suspect you're misusing inheritance to try to do this. But you can get
close by:
class AbstractFoo {
public abstract void update(Object... args);
}
class ConcreteFoo extends AbstractFoo {
public void update(Object... args) {
if (args == null || args.length != 1 || (!args[0] instanceof Integer))
throw new IllegalArgumentException("screw you!");
int mySpecificArg = ((Integer)args[0]).intValue();
...
}
}
But to do so, you're saying that any user of AbstractFoo must know the actual
subclass they're using in order to pass reasonable arguments. That sucks, and
indicates your type hierarchy is broken.
A little better might be to define an interface like FooUpdateData, and have
public abstract void update(FooUpdateData data);
in the superclass, and provide factory methods for getting the update data so
that callers can remain agnostic to the type of Foo they're using. But even
so, I'd worry that I'm getting a bit esoteric for a simple requirement.
A bit more data about what you're trying to accomplish would help to give
better answers.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
c.scherr - 10 Dec 2007 23:12 GMT
> I suspect you're misusing inheritance to try to do this.
You are right. Except for this method my superclass is fine, and i'll
probably do things differently to keep it clean.
> But you can get
> close by:
[quoted text clipped - 13 lines]
>
> }
That's basically what I was looking for, thanks.
I wasn't aware of the "Object..." possibility, is there a name to
that? (Kind of hard to search for documentation on "...")
Mark Rafn - 11 Dec 2007 00:17 GMT
>> I suspect you're misusing inheritance to try to do this.
>You are right. Except for this method my superclass is fine, and i'll
>probably do things differently to keep it clean.
Cool.
>> public abstract void update(Object... args);
>I wasn't aware of the "Object..." possibility, is there a name to
>that? (Kind of hard to search for documentation on "...")
It's equivalent to Object[] args, except a caller can specify zero or more
arguments which become the array.
The feature is called "varargs" after the ancient varargs.h C header
required for using the va_* functions to get variadic arguments. See
http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
rossum - 09 Dec 2007 22:11 GMT
>I would like to force all subclasses of a given superclass to
>implement a method public void update(...);
[quoted text clipped - 7 lines]
>
>thanks for your help.
As well as the other suggestions, you could use:
public void update(String params);
Each individual update method would have to parse its own parameter
string.
This does not solve the many problems, but it does allow you to use a
single parameter type.
rossum
c.scherr - 10 Dec 2007 23:16 GMT
> As well as the other suggestions, you could use:
>
[quoted text clipped - 7 lines]
>
> rossum
Thanks for your idea, but parsing would be too slow in my case.
Owen Jacobson - 09 Dec 2007 23:02 GMT
> I would like to force all subclasses of a given superclass to
> implement a method public void update(...);
[quoted text clipped - 5 lines]
> Is there a way to do this? It is not really congruent with the whole
> superclass concept, so maybe not...
Others have already offered suggestions on how to do this, so I'm going
to poke at the why a bit.
From a caller-via-the-superclass's point of view, all subclasses are
equivalent. For example,
public void doSomethingWithScherrUpdatable (Updatable up) {
up.update (12);
}
is valid for calls such as
doSomethingWithScherrUpdatable (new TakesOneStringUpdatable ());
and
doSomethingWithScherrUpdatable (new NoArgsUpdatable ());
The superclass's interface describes how client code calls the
subclass's methods.
Under these constraints it makes no sense to have a superclass method
whose signature is different for each subclass: code trying to call the
update method via the superclass interface would have to have knowledge
of which subclass it's calling, at which point it might as well call it
through the subclass interface and make that knowledge explicit.
Is there a reason you need the "update" method to be available via the
superclass? If not, have you thought about giving each subclass an
update method but *not* giving one to the superclass?
A concrete example of what you're building would help us come up with
more concrete suggestions for how to build it.
-o
c.scherr - 10 Dec 2007 23:39 GMT
> ...
>
[quoted text clipped - 7 lines]
> superclass? If not, have you thought about giving each subclass an
> update method but *not* giving one to the superclass?
Thanks for your answer, I thought of a different solution, that does
not misuse the super-/subclass concept and will get rid of this update
method alltogether. With my initial idea, i would just have had to pay
attention to call the right subclass with the right parameters...
Mark Space - 10 Dec 2007 01:32 GMT
> I would like to force all subclasses of a given superclass to
> implement a method public void update(...);
[quoted text clipped - 7 lines]
>
> thanks for your help.
Just to pop up here also...
I'd require the method to take no parameters. Then use setters on the
object in question to configure it so it "just knows" what to do, then
call update();
Example:
Circle c = new Circle();
c.setRadius(10);
c.setPosition(0,0);
c.update();
Square s = new Square();
s.setSize( 10, 5 );
s.setPosition( 0, 0);
s.update();
Here's two related objects are configured then "updated()", doing
whatever that does for the object. Maybe the object is drawn on the
screen, or maybe it's serialized to a file.
c.scherr - 10 Dec 2007 23:43 GMT
> > I would like to force all subclasses of a given superclass to
> > implement a method public void update(...);
[quoted text clipped - 28 lines]
> whatever that does for the object. Maybe the object is drawn on the
> screen, or maybe it's serialized to a file.
thanks. thats about what i'm going to do.
Roedy Green - 10 Dec 2007 11:23 GMT
On Sun, 9 Dec 2007 13:14:30 -0800 (PST), "c.scherr"
<constantinscherr@gmail.com> wrote, quoted or indirectly quoted
someone who said :
>but the input may be arbitrary different, for example one class may
>need the method update(int i); another one update(double a1, double
>a2); and yet another one update(double[] abc);
>
>Is there a way to do this? It is not really congruent with the whole
>superclass concept, so maybe not...
The best you could do is make an abstract class with all the variants
or a base class with dummy implementations of the variants.
I suppose you could also enforce your restriction with reflection.
see http://mindprod.com/jgloss/reflection.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
c.scherr - 10 Dec 2007 23:46 GMT
On 10 Dez., 12:23, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Sun, 9 Dec 2007 13:14:30 -0800 (PST), "c.scherr"
> <constantinsch...@gmail.com> wrote, quoted or indirectly quoted
[quoted text clipped - 15 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Thanks, I will look into "reflection".
Lew - 11 Dec 2007 02:46 GMT
Roedy Green suggested:
>> I suppose you could also enforce your restriction with reflection.
>> seehttp://mindprod.com/jgloss/reflection.html
> Thanks, I will look into "reflection".
Just to warn you, reflection ranges from simple, relatively straightforward
stuff to complicated logic fraught with peril. Be patient as you investigate
it and cautious when you use it.

Signature
Lew