Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Christopher Diggins

Dr. Dobb's Bloggers

Composition vs Inheritance

April 23, 2008

In most object-oriented code inheritance is perhaps the most fundamental technique of object reuse, but it has several flaws. Object composition is much more robust, and often more appropriate but still hasn't become popular. I beleive that this can be attributed to the fact that object composition is more verbose than it needs be. I propose the introduction of a new feature to modern OOPLs to alleviate this: delegation of interface implementation.

A feature that I haven't really seen in many languages, and whose ommission has always struck me as odd, is the ability to delegate an interface implementation. In other words the ability to declare in a single line of code, the following pseudo-code:

implement IMyInterface through field fMyField

This feature would simply be syntactic sugar, but in many circumstances can save reams of code. It would also make object composition much more attractive to programmers as a method of reusing code.

Here are some of the more prominent problems with inheritance that motivate my desire to avoid it where possible:

  • Inheritance in most OOPLs also implies subclassing/subtyping. Subtyping is not the same as inheritance (see: http://ctp.di.fct.unl.pt/mei/talmp/docs/InheritanceIsNotSubtyping.pdf ). Giving me subtyping when I want inheritance or code reuse is a bad thing. (especially in C++).
  • Function overriding can break objects in very subtle ways. For example if the preconditions are tightened, or postconditions are loosened.
  • Objects that depend on virtual function overriding to provide implementation strategies are very fragile and hard to test
  • Multiple inheritance suffers from subtle problems (e.g. the diamond problem of repeated shared base classes).
  • Forcing things into any given inheritance hierarchy is often an appropriate abstraction for one part of a system, but not for another.

Anyway, I still use inheritance because it is the best tool I've got, but I would really like delegation of interface implementation in my toolkit. Perhaps a prominent language designer on this site will consider the feature?

As a final thought, I am not much of a Ruby or Python programmer but I would be interested in learning how I could emulate this feature in Ruby and Python using their metaprogramming facilities. 

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.