1

I have an inheritance hierarchy three deep : an abstract base class, from which I derive another abstract class, call it the intermediate class, from which will derive possibly a large variety of derived classes.

At one point, the base class calls a this.GenerateXml() method, implemented in the derived class, that returns nothing, but writes xml to a public property. At this point, I would like the intermediate class to do some stuff, without the base class or the derived class having to call a specific method on the intermediate class.

Is this possible in OO languages? Constructors call each other up the chain of inheritance. Can methods? Can an object call a method on its immediate descendant? The language in this case is C#

The obvious solution is to call a method, perhaps a same named method, from the derived class, using "base". This is less than ideal, because every derived class has to remember to make the call.

closed as unclear what you're asking by gnat, david.pfx, user40980, GlenH7, Doc Brown Jun 9 '14 at 11:56

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat May 27 '14 at 9:04
  • The obvious solution is to call a method, perhaps a same named method, from the derived class, using "base". This is less than ideal, because every derived class has to remember to make the call. – bbsimonbb May 27 '14 at 9:27
  • I think you need to reconsider your design. What is wrong with protected methods? – reinierpost May 27 '14 at 13:15
7

I think, what you're looking for is the Template method pattern. You define in one abstract class the general outline of what needs to be done by calling smaller, more fine-grained methods. One (or more) of these methods is abstract and must thus be implemented by inheriting classes.

abstract class Vehicle {
    public void drive() {
        startEngine();
        goForward();
        stopEngine();
    }

    protected abstract void goForward();

    private void startEngine() {
        ...
    }

    private void stopEngine() {
        ...
    }
}

All inheriting classes implement the goForward() method while only drive() is publicly visible. Vehicle thus defines how driving in general works while a Car or Truck implements the actual movement.

Your request seems to be a little more complex by using another inheritance layer. But, in general, I don't see a problem with another intermediate abstract class that passes the abstract method even further down, or even implements it using itself the template method pattern all over again.

  • Indeed, I have a template pattern, in that my abstract base class defines then calls abstract methods which are implemented by inheriting classes. But there is still no answer as to how I might walk the tree. – bbsimonbb May 27 '14 at 14:13
  • @user1585345 Okay, then what am I missing? You can have an abstract method in your base class, implemented in your intermediate class which in turn calls an abstract method in your derived class. Can you elaborate on what's missing in order to solve your problem? – jhr May 27 '14 at 14:25
1

... the base class calls a this.GenerateXml() method, implemented in the derived class, that returns nothing, but writes xml to a public property. At this point, I would like the intermediate class to do some stuff, without the base class or the derived class having to call a specific method on the intermediate class.

When any of your classes calls "this.GenerateXml()", then your program will use the first implementation of that method that it can find, starting from the current class and working upwards through the Inheritance chain - Derived, Intermediate, Base. So, what you're looking for will only work if the derived class doesn't implement GenerateXml.

The question is, what is this "stuff" that you want to do? Is it something that might change from implementation to implementation? If so, it should be defined in the base class as a protected, virtual (not abstract) method that your Intermediate class implements. Of course, you will have to change the Base class to call this new method at the right time.

  • The work is generating an XML DOM. Some derived types will inherit directly from the base type, and their GenerateXML() can be called directly no problem. For the types that derive from the intermediate type, there appears to be no way of putting the shared work in the intermediate object and having the control pass "through" the intermediate object, without putting dedicated calls in the base, or in all the derived classes that inherit from the intermediate. – bbsimonbb May 27 '14 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.