Is it okay to implement a virtual method in a base class because I know that the majority of derived classes will use that particular implementation?
Suppose I have the following class hierarchy (in C#):
public abstract class BaseClass {
public virtual void MethodA() {
//Implement MethodA as DerivedClassA and DerivedClassB
//need it to avoid code duplication for
//DerivedClassA.MethodA and DerivedClassB.MethodB
}
}
public class DerivedClassA : BaseClass {}
public class DerivedClassB : BaseClass {}
public class DerivedClassC : BaseClass {
public override void MethodA() {
//Do MethodA in a DerivedClassC kinda way
}
}
I feel like it should be okay because this seems to be the reason that virtual methods exist but something also feels a little "smelly" about it to me. I guess I would just like confirmation that this approach is okay or to be told why its bad.