Re: Constructor inheritance
- From: "isamura" <nospam@xxxxxxxxxx>
- Date: Mon, 24 Oct 2005 21:54:51 -0400
"Andrew McDonagh" <news@xxxxxxxxxxxxxxxxxxxxx> wrote in ...
: isamura wrote:
: > "Andrew McDonagh" <news@xxxxxxxxxxxxxxxxxxxxx> wrote ...
: > : isamura wrote:
: > : > "zero" <zero@xxxxxxx> wrote ...
: > : > : "isamura" <nospam@xxxxxxxxxx> wrote in
: > : > : news:Ie6dncUBmtMdkcDeRVn-jA@xxxxxxxxxx:
: > : > :
: > : > : >
:
: snipped
:
: > : > compiler would do its magic and create all inherited constructors, when missing? I guess
this is
: > : > just my wishful thinking but we have to live with what is given. Just don't expect all of us
to
: > be
: > : > happy about it.
: > : >
: > : > ..K
: > : >
: > : >
: > :
: > : No, because we often don't want the same set of constructors on derived
: > : classes, that are available on base (super) classes.
: > :
: > Inheritance is supposed to promote code resuse (and good design).
:
: Not really, Inheritance is to provide an 'is-a' relationship between one
: derived type and its parent type, there by allowing the 'Liskov
: Substitutability Principle' to work.
:
: Code reuse is useful side effect of this relationship, but its not it
: aim. Indeed designs that make use of inheritance simply to get code
: reuse are usually the fragile designs, comprising of large inheritance
: trees.
:
Agreed. I did not mean to imply inheritance be used (abused) in this manner. I doubt code reuse can
be of much in this case.
: If code re-use is the aim, then delegation not inheritance should be
: favored.
:
: As a concrete example, consider Java's Thread class. To create and use
: a thread, a much more preferable OO design is to instantiate a Thread
: object and give it an instance of one of your own classes that implement
: the Runnable interface. This design then conforms to the 'Single
: Responsibility Principle'. Also, we still get total code re-use and a
: dynamic runtime relationship between the Thread class and the Runnable
: class.
:
Good example.
: Where as we could derived from Thread and implement the Run() method
: with the same logic that could exist within the first example's Runnable
: class.
:
A thread is a process that runs a sequence of code so your previous example makes sense. But
subclassing for the same purpose would not. It wouldn't even pass the IS-A test.
: But now we have a static compile time relationship, a Thread derived
: class that not only is a Thread but also the Runnable - its a duel
: responsibility class.
:
: Yet there's no more code reuse.
:
: > I will agree that at times it may be desirable to over-ride a behavior
: > of the super class that is not appropriate, but this does
: > (should) not happen often. At least not in my "camp".
:
: Agreed, but not what I was writing about. I was attempting to show that
: with constructors in Java we can chose to hide some or all of the same
: kind of constructors that appear on the base classes through the derived
: classes.
:
That is one point of view. Alternatively, allow subclasses to inherit constructors. For a given
class, if any of the inherited constructors are invalid, the class can over-ride the "illegal"
constructor to throw an exception to indicate it shouldn't be used. This approach may have alleviate
the OP original problem. However, it is a moot point since constructors are not allowed to inherit
by design.
: I rarely find the need to have the same number and signature type of
: constructors on derived classes - see my other post with the
: MyOwnException class deriving from RuntimeException.
:
Sure, but it does get old having to code (with/out aid of tools)
ClassB(String s) {
super(s);
}
: >
: > : Simplistically, a compiler jobs is to convert your .java source code
: > : into JVM compatible byte code.
: > :
: > So it was surprising the compiler generates missing default constructors.
:
: Not really, that is a special case dealing with the mechanics of
: creating an object - all classes need a constructor of some kind, the
: simplest one being the no-arg constructor. However, the compiler will
: only generate a default constructor if no other user defined
: constructor appears on the class. If you have created your own
: constructor, the compiler can't generate one because there is no
: universal good option available for every java application, and so the
: compile will fail with an error.
:
The "surprise" element was for a newbie (like me) to find out s/he been fooled by the compiler in
thinking constructors can sometimes be inherited.
..K
.
- References:
- Constructor inheritance
- From: Alex
- Re: Constructor inheritance
- From: Andrew McDonagh
- Re: Constructor inheritance
- From: Lasse Reichstein Nielsen
- Re: Constructor inheritance
- From: Andrew McDonagh
- Re: Constructor inheritance
- From: zero
- Re: Constructor inheritance
- From: isamura
- Re: Constructor inheritance
- From: Andrew McDonagh
- Re: Constructor inheritance
- From: isamura
- Constructor inheritance
- Prev by Date: Freelance Java programmer
- Next by Date: Re: Constructor inheritance
- Previous by thread: Re: Constructor inheritance
- Next by thread: Re: Constructor inheritance
- Index(es):
Relevant Pages
|