#353 – Why You Might Define an Abstract Class

An abstract class cannot be instantiated and is meant to serve as a base class for other classes.  It might contain implementations for its methods, which derived classes would inherit.  It might also contain one or more abstract methods, with no implementation, which must be overridden in the derived classes.

You’d typically use an abstract class when it would only make sense to instantiate the derived classes and when the base class just serves as a blueprint for the derived classes and to possibly include the implementation of some methods.

For example, you might have a Person class and Man and Woman classes that inherit from Person.  You can imagine only ever creating instances of Man and Woman and never of a generic Person–everyone is either a man or a woman.  You can imagine methods implemented in Person that are common to both men and women, like Sing.

Advertisements