Using inheritance purely to share common functionality

You are correct. Unfortunately inheritance is used a lot when it is not actually needed.

If there isn't is-a relationship between the child and parent class then inheritance should not be used.


If the parent class methods are there purely as 'utilties' then yes, I agree.

The question (for me at least), would be if the parent class could be modified in the future to have benefit. Meaning, what is the current relationship logically? If it's an "is a" between child and parent then leave it. If the parent is just a collection of methods, refactor to a utility class or use delegation.


Inheritance can be used (and abused!) in different ways. Here are the three big categories.

Conceptual hierarchy:

conceptually related classes can be organized into a specialization hierarchy :

  • people, employees, managers
  • geometric objects ...

Polymorphism:

Objects of distinct, but related classes may be uniformly treated by clients

  • array of geometric objects

Software reuse:

Related classes may share interfaces, data structures or behaviour.

  • geometric objects ...

For a complete study of the different forms of inheritance, read On the notion of inheritance.

The case that you mention, is software reuse. There is no is-a relationship, at most a has-a relationship. The goal is mostly to reuse the same code.

As you suggest, this can be refactored with delegation, or even into a utility class if the methods are essentially static.

Tags:

Oop