Re: Types of inheritance

From: Chris Smith (cdsmith_at_twu.net)
Date: 10/18/03


Date: Sat, 18 Oct 2003 15:01:52 -0600

Fu, Ren-Li wrote:
> > Fu, Ren-Li wrote:
> > > Another reason I dont like implementation inheritance is because you have to
> > > reproduce code, don't you?

> "Chris Smith" <cdsmith@twu.net> wrote:
> > No. Whenever an instance of a derived class is created, the constructor
> > chains a call (implicitly or explicitly) to the superclass. If the
> > superclass has a constructor that initializes the state of the object,
> > then the derived class will always have its inherited state initialized.

> I meant to say interface inheritance. Becase no implementation is inherrited
> (sorry for the confusion).

Just a suggestion. Though the terms "interface inheritance" is correct
per se regarding the implementing of interfaces in Java, it can be
misleading. In the context of Java, the word "inheritance" typically
refers to the complete inheritance of both implementation and interface
provided by Java's "extends" keyword. If I were speaking about Java and
meant implementing an interface, I'd just go ahead and say "implementing
an interface".

The phrase "interface inheritance" (or, more commonly heard,
"inheritance of interface" is really appropriate for more abstract
discussions where you mean to speak about a characteristic common of ALL
forms of inheritance of implementation: that is, both implementing an
interface and extending a class. (The latter technique *also* provides
inheritance of implementation, but certainly provides no lesser degree
of inheritance of interface, and trying to exclude it from the
terminology is inaccurate and misleading).

Anyhow,

> As for explicit chaining, this is not true in implementation inheritance.
>
> If it is please clarify this. But I have a class NWObject which prints a
> message in its constructor, and class Monster extends NWObject does not
> print the same message unless it explicitly calls super(). That's
> implementation inheritance for you. I don't know why I need to call super(),
> but maybe its a good thing.

Oh yes, it rather IS true of implementation inheritance. Have you
actually tried it?

If no explicit call is present to a superclass constructor in Java, then
(1) in the absence of a chained invocation of another constructor of the
current class, then an implicit call is generated; or (2) in the
presence of a chained invocation of another constructor of the current
class, the same two requirements apply recursively to that constructor.
Since a class is not allowed to have a circular series of chained
constructor calls, there is NO WAY WHATSOEVER to construct a class
without calling one of the superclass constructors in the process.

Try it. See for yourself.

> What about interface inheritance? My original
> question was if I would need to cut and paste since no implementation is
> inherrited..

Not necessarily. If you have a substantial amount of code that needs to
be duplicated, then extracting it into a utility method could work, too.
You can't put the code in the common interface (since no code can exist
in an interface), but there are other ways to share code.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: DriverManager in JDBC
    ... Why the constructor is provided ??? ... ensured that no client can instantiate the class. ... It also prevents specious inheritance of the class. ... that return implementation instances by their interface type. ...
    (comp.lang.java.programmer)
  • Re: inheritance issue, seems to be sharing data incorrectly
    ... You do not want a constructed List for the prototype of AList. ... It simply calls the function (another constructor here, ... The only real question is how is it that instanceof 'breaks' under ... the David Crockford style of inheritance seemed ...
    (comp.lang.javascript)
  • Re: Inheritance?
    ... > end of chain" comes too quickly. ... "Constructor functions" in Javascript are not part of the inheritance ... new object with the given object as its prototype. ...
    (comp.lang.javascript)
  • Re: PL/I and OOP
    ... I am considering putting into the pl1gcc version of PL/I ... public A // constructor ... protected void doit // Override method in A ... I am not saying this is a particular elegant way of using inheritance. ...
    (comp.lang.pl1)
  • Re: Subclassing, Inheritance, RE subclass "constructor" and inherited members
    ... The analogies of inheritance. ... would put constants and methods in the prototype (to avoid ... the constructor reassignment back to "Son" dirties any ... iterations I make e.g., without reassigning constructor, I get "x, y" ...
    (comp.lang.javascript)