Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using inheritance purely to share common functionality

Tags:

oop

I recently encountered a situation in some code I am working on that doesn't make sense to me. A set of classes are inheriting from a base class purely to share some methods in the base class. There is no method overriding, just child classes calling methods from the parent class.

It seems to me that this would be better modeled by having the child classes reference the parent class rather than inheriting from it, which I think would reduce unnecessary complexity. Is this reasonable, or am I missing some benefit of using inheritance like this?

like image 252
newdayrising Avatar asked Mar 10 '10 14:03

newdayrising


People also ask

When should you use inheritance?

Inheritance should only be used when: Both classes are in the same logical domain. The subclass is a proper subtype of the superclass. The superclass's implementation is necessary or appropriate for the subclass.

Why should we use inheritance?

Inheritance is one of the most important aspects of Object Oriented Programming (OOP). The key to understanding Inheritance is that it provides code re-usability. In place of writing the same code, again and again, we can simply inherit the properties of one class into the other.

When to use inheritance vs composition?

The main difference between inheritance and composition is in the relationship between objects. Inheritance: “is a.” E.g. The car is a vehicle. Composition: “has a.” E.g. The car has a steering wheel.

What is difference between composition and inheritance in C++?

Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects. Whereas inheritance derives one class from another, composition defines a class as the sum of its parts.


3 Answers

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.

like image 62
derivation Avatar answered Nov 03 '22 11:11

derivation


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.

like image 38
Tuomas Pelkonen Avatar answered Nov 03 '22 11:11

Tuomas Pelkonen


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.

like image 27
ewernli Avatar answered Nov 03 '22 11:11

ewernli