Re: Types of inheritance
From: Chris Smith (cdsmith_at_twu.net)
Date: 10/18/03
- Next message: Bob X: "Re: Java as a first language"
- Previous message: Bob X: "Mentoring?"
- In reply to: Fu, Ren-Li: "Re: Types of inheritance"
- Next in thread: Chris Smith: "Re: Types of inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Bob X: "Re: Java as a first language"
- Previous message: Bob X: "Mentoring?"
- In reply to: Fu, Ren-Li: "Re: Types of inheritance"
- Next in thread: Chris Smith: "Re: Types of inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|