#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
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #446 – Deciding Between an Abstract Class and an Interface

  1. Sean, I regularly re-review your posts in here, not only to refresh on helpful tidbits, but also for the humor. I love that you have the Woman class implement IAskForDirections! I also got a kick out of the drill sergeant bark!

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 )

Connecting to %s

%d bloggers like this: