Its usefulness is greatly exaggerated. As a general design rule of thumb: If there's any question about whether inheritance is appropriate, it's not.
It should be used only for is-a relationships, never for has-a relationships.
How should we use inheritance in the following example?
Name
class for representing a
person's name (first, middle, last, suffix). We want a
Person
class.
Rectangle
class for representing a
rectangular area in Cartesian coordinates. We want a class to represent
a button on the screen. The button has the same properties as
a Rectangle
, as well as a text label.
Book
class for representing books in
a library, but some books are part of a larger series (e.g.,
encyclopedia volumes), and we want each to have an instance
variable representing the series of which it is part.
SavingsAccount
class. The
CheckingAccount
class can do everything the
SavingsAccount
can do, but it also has a
deductCheck
method.