I’ve created a parent class that has common methods, but there are methods that I want to be implemented by child classes. i.e. Click Execution methods.
What is the proper way to implement this?
public Parent {
CancelCommand = new RelayCommand(CancelClicked, CancelCanExecute);
private void CancelClick(object param) {
// Do stuff
}
}
public Child : Parent {
private bool CancelCanExecute(object param) {
// Do stuff
return true;
}
}
Where CancelClick is hosted by the parent class, and CancelCanExecute is handled by the child class.
Would I use an extern? Or should I use interfaces? If I use override in the child class, it doesn’t force me to have to implement the method. I’m not sure I should change the parent class to an abstract because it has non-abstract methods and such.
So, here are your options:
1. Declare your base class as
abstractand some methods as wellThis approach has two good points: you will be free to implement common methods at the base class (that is, not all of them need to be
abstract) while any abstract method will must be overridden at derived classes. There is one counter point (that you may be aware of): you can’t instantiate it. That is, you can’t do something like:However, you stil will be able to do this:
2. Use interfaces
You may declare some interfaces to force your classes to implement some methods. However, the semantics between inheritance and interface is quite different. You must decide which is best for your.
IMHO, you would be fine with the first option.