Re: Base class - Derived class interaction question



On Mar 11, 9:59 am, "Chris Uppal" <chris.up...@xxxxxxxxxxxxxxxxxxx
THIS.org> wrote:
Daniel wrote:
What's
throwing me off is that the plus method has to return a reference of
the _derived_ type, which is seemingly impossible if I'm implementing
the method in the base class.

If you think about it you'll see that this is impossible. You want to do two
incompatible things at once

- inherit the code from the superclass

- have that inherited code do something different

There /are/ languages which work like that (where inherited stuff is not merely
inherited but redefined in the subclass), but Java doesn't have the right kind
of type system for that.

The real problem is that the code which creates a new instance does not refer
to the (runtime) class of "this" but the compile-time class where the code is
defined. Since constructors don't inherit it follows that if you want do this
at all (in Java) you /cannot/ use an explicit constructor in the shared code.
(One more example of how explicit constructors cause fragility.)

Following that thought, the nearest you can get is by refactoring to split the
creation of the new object into something that the subclass can override. For
instance, you could make use of the built-in clone() operation, like:

class Base
implements Cloneable
{
Base plus(Base operand)
{
// idiotic try/catch elided for clarity
Base retval = (Base)(this.clone());
retval.value += operand.value;
return retval;
}
}

And then, in the subclass you can override add() to return a Derived, if you
want:

class Derived
extends Base
{
Derived plus(Base operand)
{
return (Derived)(super.plus(operand));
}
}

-- chris

Thanks for the ideas. I like the idea of assigning the object
creation to a construct that can pick the appropriate constructor at
runtime (like the clone method) as oppose to to using the constructor,
which, as Chris pointed out, is doomed to use the base class code even
if the calling instance is of derived type.

I would still be interested as to whether there are any design
patterns or generics tricks that I could use to accomplish similar
means without having to change or add any code to the derived classes
at all. Not an urgent request or anything, just curious (not that it
being urgent would change things from other posters' POVs :-)).

I appreciate everyone's help!

- Daniel

.



Relevant Pages

  • Re: Comments on code layout requested
    ... Those should usually be moved to and initialized through a prototype object. ... You can try applying the suggestions on closures in the FAQ Notes. ... let other objects inherit from a structure like this? ... could be avoided by adding a check to the Spinner constructor. ...
    (comp.lang.javascript)
  • Does form OnCreateOrder property have a bug? I think so.
    ... UBaseForm) from which the third and fourth forms will inherit. ... When you execute this and call the manually created form, the constructor fires ... procedure TMainForm.Button2Click(Sender: TObject); ...
    (borland.public.delphi.ide)
  • Re: inheritance in JavaScript
    ... is it possible to inherit from a native object? ... instance of that object to the prototype of a constructor. ... assigning a reference to another constructor's prototype. ...
    (comp.lang.javascript)
  • Re: Erben von Image
    ... As YourClass' since it forces to inherit from that class. ... Or you can change the scope of your class's constructor ... So the solution is using the MustInherit keyword and also ... This way you meet the three goals: ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Constructor inheritance (or lack thereof)
    ... forced to have a parameterless constructor. ... > match a constructor with the same parameters in the base class call ... public class Child: Parent ... >> have wanted to inherit a constructor. ...
    (microsoft.public.dotnet.languages.csharp)

Loading