The Artima Developer Community
Sponsored Link

Inheritance and IS-A



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Inheritance and IS-A

Posted by Bill Venners on 25 Oct 1998, 6:48 PM


Thanks very much for this feedback. I really like this kind of
constructive criticism.

You made two points that hadn't occured to me before and on
which I agree with you:

  • Compared to inheritance, composition results in more
    objects getting instantiated, which can incur a performance
    cost at both instantiation and GC time.

  • Compared to inheritance, the explicit method delegation
    required by composition results in more code that has to
    be written, debugged, maintained, and (I might add) fit
    in memory at run-time.

These two points further highlight composition's performance
inferiority compared to inheritance, and I'll include them in
the book.

I'm not sure what you mean by "the importance of type
compatibility as a benefit of inheritance," and would welcome
some elaboration on that point. Perhaps you mean polymorphism
and dynamic binding, but you can also get these if you use
composition with interfaces. If you mean plain old compile-
time and run-time type checking, I don't see where inheritance
gives us any advantage over composition. The compiler and VM
will ensure that given a variable of type Zebra, I'll only
be able to invoke accessible methods declared in class Zebra,
whether Zebra is a front-end class or a superclass. To me,
the advantage of the type compatibility between subclass and
superclass is polymorphism and dynamic binding, which as I
said, is also available to composition relationships via

As far as your impression that my article has a bias towards
composition, you are probably right and if so that means I
need to rework my presentation. The two main points I wanted
to make in this article are:

  1. In the composition versus inheritance contest, composition
    generally wins on flexibility and inheritance on performance.

  2. My advice in choosing between composition and inheritance
    is: If you have a permanent is-a relationship, use
    inheritance. Else, use composition.

I don't avoid inheritance in my designs, I just don't use
inheritance unless I have an is-a relationship. My
reason for highlighting the flexibility advantages of
composition is to encourage the use of compositiion for any
non-is-a relationship. Your feedback makes me realize that I
need to make this clearer in my book.

I would be interested in hearing how you and anyone else feels
about my use of the is-a relationship as a
litmus test for whether or not to use inheritance. I think that
when I design, I sort of do a lot of things by feel.
It isn't always easy to describe in words how I go about
making design choices, but this is-a guideline I think
fairly well captures my own personal approach.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us