You are on page 1of 4

Recommendations for Abstract Classes vs.

Interfaces
The choice of whether to design your functionality as an interface or an abstract class (a MustInherit class in Visual Basic) can sometimes be a difficult one. An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes. An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer. Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument. For example: ' Visual Basic Public Sub Spin (ByVal widget As IWidget) End Sub // C# public void Spin (IWidget widget) {} This method could accept any object that implemented IWidget as the widget argument, even though the implementations of IWidget might be quite different. Abstract classes also allow for this kind of polymorphism, but with a few caveats:

Classes may inherit from only one base class, so if you want to use abstract classes to

provide polymorphism to a group of classes, they must all inherit from that class.

Abstract classes may also provide members that have already been implemented.

Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface. Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.

If you anticipate creating multiple versions of your component, create an abstract class.

Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

If the functionality you are creating will be useful across a wide range of disparate

objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

If you are designing small, concise bits of functionality, use interfaces. If you are

designing large functional units, use an abstract class.

If you want to provide common, implemented functionality among all implementations of

your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

Abstract Classes
Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code. An abstract class is denoted in Visual Basic by the keyword MustInherit. In C#, the abstract modifier is used. Any methods that are meant to be invariant may be coded into the base class, but any methods that are to be implemented are marked in Visual Basic with the MustOverride modifier. In C#, the methods are marked abstract. The following example shows an abstract class: // C# abstract class WashingMachine

{ public WashingMachine() { // Code to initialize the class goes here. }

abstract public void Wash(); abstract public void Rinse(int loadSize); abstract public long Spin(int speed); } In the above example, an abstract class is declared with one implemented method and three unimplemented methods. A class inheriting from this class would have to implement the Wash, Rinse, and Spin methods. The following example shows what the implementation of this class might look like: // C# class MyWashingMachine : WashingMachine { public MyWashingMachine() { // Initialization code goes here. }

override public void Wash() { // Wash code goes here. }

override public void Rinse(int loadSize) { // Rinse code goes here. } override public long Spin(int speed) { } } When implementing an abstract class, you must implement each abstract (MustOverride) method in that class, and each implemented method must receive the same number and type of arguments, and have the same return value, as the method specified in the abstract class. // Spin code goes here.

You might also like