Refactoring Tips - Tip 10
Tip 10
If the derived classes(or concrete/specialized classes) cant make use of all the functionalities (100% usage) of its base class(though it is a 'is a kind of' relationship among them, then prefer delegation/composition over Generalization/inheritance.
Bad practice
public abstract class BaseClass
{
abstract void Method1();
abstract void Method2();
}
public class Derived : BaseClass
{
public override void Method1()
{
}
public override void Method2()
{
}
}
Good practice
public class BaseClass
{
public void Method1()
{
}
public void Method2()
{
}
}
public class Derived
{
private BaseClass objBase;
public Derived():this(new BaseClass())
{
}
public Derived(BaseClass obj)
{
objBase = obj;
}
public void Method1()
{
objBase.Method1();
}
}
I hope this helps!.
Regards,
-Vinayak