Skip to content

Single Inheritance and Multiple Inheritance Explained

Matt Zumwalt edited this page Nov 25, 2015 · 3 revisions

Single Inheritance and Multiple Inheritance Explained

Note: These images are from a slide deck that Matt Zumwalt made for the initial PCDM Sprint in 2015. If you need to edit or re-use them, feel free to do so.

Single inheritance is the classic object-oriented way to share functionality across mutliple classes.

Multiple Inheritance (aka "Mixins") is a common approach in Ruby. This pattern occurs in other programming languages, but it is especially prevalent with Ruby.

All of the hydra components emphasize multiple inheritance internally but tend to provide "Base" classes for consumers to inherit from in case they prefer Single Inheritance. These diagrams show how the hydra-pcdm and hydra-works gems support both approaches.

The Problem: Connecting the Dots

What are the different ways you can get the desired behaviors to cascade from top to bottom so that they all will be present in the classes at the bottom (ie. Sufia::GenericWork, Dissertation, etc)?

Diagram: Single Inheritance vs. Multiple Inheritance - Connect the Dots

Single Inheritance

This approach uses single inheritance, but the actual behaviors are all in modules that could be mixed together using multiple inheritance.

Diagram: Single Inheritance

Multiple Inheritance (aka "Mixins")

This approach uses only mixins/modules, with no Superclass relationship. Though PCDM:Object, Works:BaseWork and Sufia:GenericWork are available for others to use as superclasses, this implementation doesn’t use them.

Diagram: Multiple Inheritance

Example: Use of Single Inheritance when Both Options are Available

This shows how something downstream of hydra-pcdm and hydra-works can choose to use single inheritance without this blocking others from using multiple inheritance (mixins).

Diagram: Example use of Single Inheritance

Clone this wiki locally