views:

118

answers:

5

I have a general question about inheritance in the .NET framework, lets say you have 2 classes, the first is called Parent and the second is called Child. Child inherits from Parent.

Parent wants to ensure that each instance of child executes a specific piece of code when it loads irrespective of whether the child has their own onLoad code explicitly specified.

From my experience, if i handle onLoad in the parent and not in the child, the parents onLoad code will fire but if its handled in both classes only the child's code will fire.

Is this correct? and if so how can i ensure the parents code will always fire for the child...

+2  A: 

In C#, use base.methodNameHere() from the child to call methods on the parent class.

In this case, call base.onLoad() from your child's onLoad() method to execute the parent's code.

ChessWhiz
Thanks yeah i thought that was the best way but wanted to see if there was a way of doing that without having to call base.something..
Grant
+2  A: 

Instead of calling onLoad() straight away, call a private function, something like beforeLoad(). This function would do all the stuff you want parent to do and then it would call onLoad() which can be overriden in the Child class.

Duracell
+6  A: 

You have discovered the fragile base class problem. One way to deal with it is this pattern:

public class Parent
{
    public void OnLoad()
    {
        // Important load stuff goes here
        OnLoadInternal();
    }

    protected virtual void OnLoadInternal()
    {
        // No implmentation in parent; child can override if needed
    }
}

public class Child : Parent
{
    protected override void OnLoadInternal()
    {
        base.OnLoadInternal();

        // Important load stuff, but can't break parent by omitting base call
    }
}

The parent is guaranteed to have it's OnLoad method do what it is supposed to do, but the child is given a way to add additional processing by overriding OnLoadInternal.

jkohlhepp
+2  A: 

If you wire the behaviour in the default constructor of the base class, and perform the behaviour in a private method, there is no way for child classes to override. You lose the ability to control this behaviour in the child class and when (in what order) the respective handlers execute.

public partial class Parent : Form
{
    public Parent()
    {
        InitializeComponent();
        Load += ParentLoad;
    }

    private void ParentLoad(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}
Jay
+1  A: 

The two established patterns in .NET SDK are virtual functions and events.

Virtual Functions

class Parent {
  virtual protected void OnLoad() {
    //code here
  } 
}

class Child {
  protected override void OnLoad() {
    base.OnLoad();
  }
}

In this scenario you give the child class the option to fully override the OnLoad behaviour, including the ability to not execute the base class OnLoad (which could be a completely legitimate case).

Using events

Alternatively you can use events, which is a way to get the child class to do additional work, without the option of overriding base class behaviour:

class Parent {
  public event Load;
  public Parent() {
    this.Load+=OnLoad();
  }
  private void OnLoad() {
    //Parent code here
  } 
}

class Child {
  public Child() {
    this.Load+=OnLoad();
  }
  private void OnLoad() {
    //Child code here
  }
}

This is really design choice. If you want the framework to help you with enforcing execution of parent code, go for option 2.

Igor Zevaka