#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