#446 – Deciding Between an Abstract Class and an Interface

An abstract class is a base class that may have some members not implemented in the base class, but only in the derived classes.  An interface is just a contract that a class can choose to fulfill–a list of member that it must implement if it implements the interface.  (Differences Between an Interface and an Abstract Class).

You’ll often use an abstract class when you use nouns to describe the is-a relationship between a base class and derived classes.  E.g. Person as an abstract base class and Man and Woman as derived classes.

By contrast, you’ll often use interfaces when you think about verbs that describe things that instances of a class can do.  E.g. IRun interface containing Run method, implemented by Man and Woman, but also Horse classes.

    public abstract class Person
    // ...

    public class Man : Person, IOperateTools, IDriveTractor

    // ...

    public class Woman : Person, IAskForDirections, ICollaborate
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: