.Net: Important Scenario for Abstract Class and Interface

Hi .Net lovers,

I assume you all know what is an Abstract class and what is an Interface, and the question always comes in mind when to use-which:

The most important scenario of deciding when to use Abstract Class or an Interface comes by trying to establish an IS-A relationship (abstract class) or CAN-DO relationship (Interface) between the related objects.

1. Abstract Class e.g.
A "Manager" IS AN "Employee" and a "BusinessAnalyst" IS ALSO AN "Employee"
so for this relationship we can create an "Employee" Abstract class and the other classes "Manager" and "BusinessAnalyst" should derive from it and may provide their own implementations...........

2. Interface e.g.
There will an annual appraisal for all types of Employees in an Organization. so, all Employees must establish a certain kind of contract to fulfill for Appraisal which is done by an "IAppraisal" interface which might have 2 methods - viz.
  • public void IncreaseGrade(); //The Manager will be promoted by a different Grade than other types of Employees
  • public void IncreaseSalary(double percentage); //The %age increase in Manager's Salary will be different (may depend on new Grade also) than other types of Employees

Hope this clarifies the scenario.

Other aspects to decide are Versioning and Ease of Use (like the existing Abstract class provides some implementation and change in its implementation code does not cause the derived classes to recompile while a change in contract or interface causes the child class to be recompiled) between the two.

Ref: Applied Microsoft .Net Framework (Jeffrey Richter )

Thanks,

::VJSS::

Comments

Popular Posts