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
Motivated achiever who guides organizations in applying technology to business settings, provides added value, and creates project deliverables in a timely manner. An experienced Technical Consultant, have successfully led large project teams of more than 20 people from requirements gathering to implementation and support using C#, .NET ,ADO.NET, ADO.NET Entity Framework,ASP.NET,ASP.NET MVC, WCF and SQL Server.