Base Classes with Mandatory Features

Problem
What’s the best course of action when you can’t implement a feature in a base class but derived classes must have it?

Solution
In this scenario, make the base class and the corresponding method abstract.

Example

public abstract class File {
    // The base class can't implement Read even though it's required.
    public abstract void Read();
}

public class TextFile : File {
    public override void Read() {
        // Implement the Read feature here.
        ...
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s