Abstract base class over interface

Currently I'm reading the book Framework Design Guidelines. This is one of the best books I have read in some time. Most books that cover design in general are one-sided and high-lights the author's beliefs and convictions. However, this book is very different. It gives suggestions about various framework design aspects and at the same time high-lights views/opinions of different people in the .NET team which doesn't necessarily conform to the guidelines. This makes the book a very interesting read.

One of the suggestions is "Favor defining classes over interface". While this is highly debatable, I agree to this in general. In this section I read a comment from Brian Pepin that reminded me of some framework code I saw long time back which convinced me that Abstract Base Classes are sometimes much superior to interfaces in defining contracts.

In that UI framework one of the requirements was that individual controls should support loading bitmaps that act as application skins with the following method prototypes....

 void LoadBitmap(string fileName);
void LoadBitmap(string fileName, Color transparentCol);
void LoadBitmap(string fileName, int width, int height);
void LoadBitmap(string fileName, Color transparentCol,                                  int width, int
 height); 

The last method is the actual implementation and all the other methodsĀ fill in default values and passes it to this method.

In case this was defined in an interface as in

 public interface ILoadBitmap{
    void LoadBitmap(string fileName);
    void LoadBitmap(string fileName, Color transparentCol);
    void LoadBitmap(string fileName, int width, int height);
    void LoadBitmap(string fileName, Color transparentCol, 
                    int width, int height);
}

All classes that implemented the interface had to do method parameter validation for all the 4 methods and call the last method passing the default value for the parameters not specified. Not only this becomes tedious if someĀ 20 types of controls supported skinning, it leads to programmer error in which a wrong default value is passed in some of the controls. This is where Abstract Base Class comes in. Using ABC you can code this as

 public abstract class SkinControl{
    public void LoadBitmap(string fileName)
    {
        Debug.Assert(!string.IsNullOrEmpty(fileName));
        LoadBitmap(fileName, Color.Magenta, -1, -1);
    }
    public void LoadBitmap(string fileName, Color transparentCol)
    {
        Debug.Assert(!string.IsNullOrEmpty(fileName));
        LoadBitmap(fileName, transparentCol, -1, -1);
    }
    public void LoadBitmap(string fileName, int width, int height)
    {
        Debug.Assert(!string.IsNullOrEmpty(fileName));
        Debug.Assert(width > 0 && height > 0);
        LoadBitmap(fileName, Color.Magenta, width, height);
    }
    public abstract void LoadBitmap(string fileName, 
                                    Color transparentCol,
                                    int width, int height);
}

public class SkinnedButton : SkinControl{
    public override void LoadBitmap(string fileName, 
                                    Color transparentCol, 
                                    int width, int height)
    {
         // Actual implementation 


    }
}

All the methods are implemented in the ABC and only the last method is made abstract. So for all classes that implements this abstract class the developer needs to implement the fourth overload of the method only. Most of the contract is directly coded into the ABC. This results in less code and less programming error.