Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #2904 > unrolled thread
| Started by | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
|---|---|
| First post | 2011-04-06 20:48 +0000 |
| Last post | 2011-04-08 20:02 +0200 |
| Articles | 11 on this page of 51 — 15 participants |
Back to article view | Back to comp.lang.java.programmer
calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-06 20:48 +0000
Re: calling own methods from constructor Tom Anderson <twic@urchin.earth.li> - 2011-04-06 23:06 +0100
Re: calling own methods from constructor Lew <noone@lewscanon.com> - 2011-04-06 18:24 -0400
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 08:44 +0000
Re: calling own methods from constructor Robert Klemme <shortcutter@googlemail.com> - 2011-04-07 04:14 -0700
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 12:32 +0000
Re: calling own methods from constructor Robert Klemme <shortcutter@googlemail.com> - 2011-04-07 05:55 -0700
Re: calling own methods from constructor Lew <lew@lewscanon.com> - 2011-04-07 08:10 -0700
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 20:31 +0000
Re: calling own methods from constructor Lew <lew@lewscanon.com> - 2011-04-07 14:10 -0700
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 22:15 +0000
Re: calling own methods from constructor Lew <noone@lewscanon.com> - 2011-04-07 21:22 -0400
Re: calling own methods from constructor Tom Anderson <twic@urchin.earth.li> - 2011-04-07 23:08 +0100
Re: calling own methods from constructor Lew <noone@lewscanon.com> - 2011-04-07 21:24 -0400
Re: calling own methods from constructor "Mike Schilling" <mscottschilling@hotmail.com> - 2011-04-09 22:06 -0700
Re: calling own methods from constructor Tom Anderson <twic@urchin.earth.li> - 2011-04-07 23:10 +0100
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 22:24 +0000
Re: calling own methods from constructor Lew <noone@lewscanon.com> - 2011-04-07 07:19 -0400
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 12:24 +0000
Re: calling own methods from constructor Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-06 20:46 -0400
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 08:36 +0000
Re: calling own methods from constructor Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-07 07:16 -0300
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 11:26 +0000
Re: calling own methods from constructor Paul Cager <paul.cager@googlemail.com> - 2011-04-07 03:20 -0700
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 11:36 +0000
Re: calling own methods from constructor Owen Jacobson <angrybaldguy@gmail.com> - 2011-04-06 22:23 -0400
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 09:13 +0000
Re: calling own methods from constructor Tobias Blass <tobiasblass@gmx.net> - 2011-04-07 10:58 +0000
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 12:19 +0000
Re: calling own methods from constructor Alessio Stalla <alessiostalla@gmail.com> - 2011-04-07 10:40 -0700
Re: calling own methods from constructor Tobias Blass <tobiasblass@gmx.net> - 2011-04-07 19:00 +0000
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 20:11 +0000
Re: calling own methods from constructor Jim Janney <jjanney@shell.xmission.com> - 2011-04-07 14:36 -0600
Re: calling own methods from constructor Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-07 20:59 -0400
Re: calling own methods from constructor Jim Janney <jjanney@shell.xmission.com> - 2011-04-07 10:26 -0600
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 20:16 +0000
Re: calling own methods from constructor Lew <lew@lewscanon.com> - 2011-04-07 14:05 -0700
Re: calling own methods from constructor Jim Janney <jjanney@shell.xmission.com> - 2011-04-07 16:15 -0600
Re: calling own methods from constructor Lew <noone@lewscanon.com> - 2011-04-07 21:28 -0400
Re: calling own methods from constructor Owen Jacobson <angrybaldguy@gmail.com> - 2011-04-07 23:21 -0400
Re: calling own methods from constructor Owen Jacobson <angrybaldguy@gmail.com> - 2011-04-07 23:39 -0400
Re: calling own methods from constructor Owen Jacobson <angrybaldguy@gmail.com> - 2011-04-07 23:58 -0400
Re: calling own methods from constructor Tom Anderson <twic@urchin.earth.li> - 2011-04-08 20:32 +0100
Re: calling own methods from constructor Tom Anderson <twic@urchin.earth.li> - 2011-04-07 23:05 +0100
Re: calling own methods from constructor Jim Janney <jjanney@shell.xmission.com> - 2011-04-07 17:38 -0600
Re: calling own methods from constructor Roedy Green <see_website@mindprod.com.invalid> - 2011-04-07 03:20 -0700
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-07 11:32 +0000
Re: calling own methods from constructor Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-08 01:51 +0200
Re: calling own methods from constructor Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-08 01:56 +0200
Re: calling own methods from constructor Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-08 09:30 +0000
Re: calling own methods from constructor Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-08 20:02 +0200
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Owen Jacobson <angrybaldguy@gmail.com> |
|---|---|
| Date | 2011-04-07 23:39 -0400 |
| Message-ID | <2011040723392523211-angrybaldguy@gmailcom> |
| In reply to | #2971 |
On 2011-04-07 23:21:39 -0400, Owen Jacobson said: > C++'s objection initialization What a revealing typo. -o
[toc] | [prev] | [next] | [standalone]
| From | Owen Jacobson <angrybaldguy@gmail.com> |
|---|---|
| Date | 2011-04-07 23:58 -0400 |
| Message-ID | <2011040723580578827-angrybaldguy@gmailcom> |
| In reply to | #2971 |
On 2011-04-07 23:21:39 -0400, Owen Jacobson said: > C++'s objection initialization What a revealing typo. -o
[toc] | [prev] | [next] | [standalone]
| From | Tom Anderson <twic@urchin.earth.li> |
|---|---|
| Date | 2011-04-08 20:32 +0100 |
| Message-ID | <alpine.DEB.2.00.1104082028050.20175@urchin.earth.li> |
| In reply to | #2971 |
On Thu, 7 Apr 2011, Owen Jacobson wrote: > Could it have been designed differently? Sure. Java's constructor > semantics are a weird-but-mostly-intuitive mix of C++'s constructors and > Smalltalk's initializers-are-just-methods approach (where you're not > forced to chain to a parent class's initializer at all). Since we're talking other languages, i'll mention Python, where in a typical chained constructor: class Subprise(Surprise): def __init__(self): super(Subprise, self).__init__() not only is the call to the superclass __init__ method a perfectly normal call, so is the use of super()! tom -- the logical extension of a zero-infinity nightmare topology
[toc] | [prev] | [next] | [standalone]
| From | Tom Anderson <twic@urchin.earth.li> |
|---|---|
| Date | 2011-04-07 23:05 +0100 |
| Message-ID | <alpine.DEB.2.00.1104072303480.25031@urchin.earth.li> |
| In reply to | #2937 |
On Thu, 7 Apr 2011, Jim Janney wrote:
> Owen Jacobson <angrybaldguy@gmail.com> writes:
>
>> public class Foo {
>> public Foo() {
>> // internalInit is private, therefore final
>> this.internalInit();
>> }
>>
>> public /* virtual */ void virtualMethod() {
>> System.out.println("Override me! I dare you.");
>> }
>>
>> private void internalInit() {
>> // Whups! 'this' is not always fully initialized.
>> this.virtualMethod();
>> }
>> }
>
> public class Bar extends Foo {
> public Bar() {
> super();
> System.out.println("Two guys walk into a...");
> }
>
> @override
> public void virtualMethod() {
> throw new Exception("the bar is closed");
> }
> }
>
> In Java evaluating new Bar() will throw an exception. But in C++ the
> equivalent code would print
>
> Override me! I dare you.
> Two guys walk into a...
>
> In effect, until the constructor of Foo completes, the object is
> considered to be an instance of Foo, so calls to virtualMethod() to to
> Foo.virtualMethod even if it has been overridden. After the super
> constructor completes, the object is treated as an instance of Bar, so
> evaluating new Bar().virtualMethod() would print two lines and then
> throw an exception.
I recoil in horror.
> I've been surprised by this behaviour in C++ enough times that I'm not
> sure that it has the better approach. But a solution does exist.
That's slightly like solving toothache by amputating your head.
tom
--
Many of us adopted the File's slang as our own, feeling that we'd found a
tangible sign of the community of minds we'd half-guessed to be out there.
[toc] | [prev] | [next] | [standalone]
| From | Jim Janney <jjanney@shell.xmission.com> |
|---|---|
| Date | 2011-04-07 17:38 -0600 |
| Message-ID | <2psjtt639s.fsf@shell.xmission.com> |
| In reply to | #2949 |
Tom Anderson <twic@urchin.earth.li> writes: >> In effect, until the constructor of Foo completes, the object is >> considered to be an instance of Foo, so calls to virtualMethod() to >> to Foo.virtualMethod even if it has been overridden. After the >> super constructor completes, the object is treated as an instance of >> Bar, so evaluating new Bar().virtualMethod() would print two lines >> and then throw an exception. > > I recoil in horror. It's surprised me more than once. You'd think after the first time I would remember, but no... > >> I've been surprised by this behaviour in C++ enough times that I'm >> not sure that it has the better approach. But a solution does >> exist. > > That's slightly like solving toothache by amputating your head. C++ is an interesting language. The interesting languages aren't always the ones you want to do actual work in. -- Jim Janney
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-04-07 03:20 -0700 |
| Message-ID | <9t3rp6l38hju8at618b4nub16s90pdqn8d@4ax.com> |
| In reply to | #2904 |
On 06 Apr 2011 20:48:41 GMT, Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> wrote, quoted or indirectly quoted someone who said : >Is there any *good* use of having the constructor call a method >that actually *can* be overridden in a subclass? I have accidentally nailed myself by overriding a method used in a constructor. I have yet to find a legitimate use for it. It is too bad the compiler does not complain about it. -- Roedy Green Canadian Mind Products http://mindprod.com Doing what the user expects with respect to navigation is absurdly important for user satisfaction. ~ anonymous Google Android developer
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
|---|---|
| Date | 2011-04-07 11:32 +0000 |
| Message-ID | <slrnipr86s.phi.avl@gamma.logic.tuwien.ac.at> |
| In reply to | #2920 |
Roedy Green <see_website@mindprod.com.invalid> wrote: > On 06 Apr 2011 20:48:41 GMT, Andreas Leitgeb ><avl@gamma.logic.tuwien.ac.at> wrote, quoted or indirectly quoted > someone who said : > >> Is there any *good* use of having the constructor call a method >> that actually *can* be overridden in a subclass? > > I have accidentally nailed myself by overriding a method used in a > constructor. I have yet to find a legitimate use for it. It is too > bad the compiler does not complain about it. I haven't (yet) been bitten by it, myself. I just recently came to think about it.
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2011-04-08 01:51 +0200 |
| Message-ID | <inlilu$q62$1@dont-email.me> |
| In reply to | #2904 |
On 06/04/2011 22:48, Andreas Leitgeb allegedly wrote:
> There is well-known danger in calling own methods from the
> constructor, namely that the method called may be overridden by a
> subclass, which is really instanciated, but whose specific
> constructor has not yet been run.
Definitely present.
> Is there any *good* use of having the constructor call a method that
> actually *can* be overridden in a subclass? I mean, are there
> (non-anti)patterns of explicitly allowing subclasses to hook into
> base-class's construction?
I've recently written something like this:
class BaseClass implements java.io.Externalizable {
protected Map<?, ?> store;
protected BaseClass( Map<?, ?> store ){
this.store = store;
initialise0();
}
public BaseClass(){ } //needed for Externalizable
protected void initialise0(){}
public void readExternal( ObjectInput input ){
this.store = (Map<?, ?>) input.readObject();
initialise0();
}
}
class SubClass {
private static final Object KEY = ...
String datum;
public SubClass( Map<?, ?> store ){
super( store );
}
protected void initialise0(){
super.initialise0();
datum = (String) store.get( KEY );
}
}
I consider this a valid use. Sure, even at this basic level you need to
be careful, especially if you get back to the code after a while and
start extending the hierarchy.
I could have duplicated the initialisation code in the (Map<?,?>) c'tor.
I'm not very fond of big c'tors however, and even less fond of code
duplication. So there.
As for the fact that you can call virtual methods, I'm all for it, just
as I am against any sensible restriction. We're all adults (probably)
and know what we're doing (mostly), and anyway there's already so many
more places you can screw up if you're not careful (autoboxing comes to
mind) -- I generally pay a lot more attention when I craft a class
hierarchy than when I write a single statement involving primitives and
Objects at the same time.
--
DF.
An escaped convict once said to me:
"Alcatraz is the place to be"
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2011-04-08 01:56 +0200 |
| Message-ID | <inlivi$q62$2@dont-email.me> |
| In reply to | #2957 |
On 08/04/2011 01:51, Daniele Futtorovic allegedly wrote:
> class BaseClass implements java.io.Externalizable {
> protected Map<?, ?> store;
> protected BaseClass( Map<?, ?> store ){
> this.store = store;
> initialise0();
> }
> public BaseClass(){ } //needed for Externalizable
>
> protected void initialise0(){}
>
> public void readExternal( ObjectInput input ){
> this.store = (Map<?, ?>) input.readObject();
> initialise0();
> }
> }
>
> class SubClass {
> private static final Object KEY = ...
> String datum;
> public SubClass( Map<?, ?> store ){
> super( store );
> }
>
> protected void initialise0(){
> super.initialise0();
> datum = (String) store.get( KEY );
> }
> }
'class SubClass *extends BaseClass*', of course.
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
|---|---|
| Date | 2011-04-08 09:30 +0000 |
| Message-ID | <slrniptldk.phi.avl@gamma.logic.tuwien.ac.at> |
| In reply to | #2958 |
Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> wrote:
> On 08/04/2011 01:51, Daniele Futtorovic allegedly wrote:
>> class BaseClass implements [...] {
In principle this is the same kind of example as Tom's.
It has already been established by now, that this calling
of virtual methods from the c'tor *can* be used with caution
such that it doesn't necessarily lead to bugs.
Also, that alternatives exist, even though they may appear
to be less convenient at times.
What's left open and would interest me is, if this pattern is
already used in any of the big Java-based projects. E.g. a publically
accessible javadoc of the API of a lib documenting some protected
method to be "called from some superclass constructor and thus
the implementation should not assume 'this' instance to be
initialized to any degree."
Not that this existence or non-existence would have any real
impact on Java's future, of course...
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Date | 2011-04-08 20:02 +0200 |
| Message-ID | <innik8$862$1@dont-email.me> |
| In reply to | #2989 |
On 08/04/2011 11:30, Andreas Leitgeb allegedly wrote:
> Daniele Futtorovic<da.futt.news@laposte-dot-net.invalid> wrote:
>> On 08/04/2011 01:51, Daniele Futtorovic allegedly wrote:
>>> class BaseClass implements [...] {
>
> In principle this is the same kind of example as Tom's.
No. In Tom's example, the subclass provided functionality to the
superclass. In my example, the subclass adds to what the superclass does.
I also presented that example to illustrate how implementing the
Externalizable contract can impact the issue. Because Externalizable
mandates a no-arg c'tor, some mechanisms one might have used in similar
cases cannot be used there.
--
DF.
An escaped convict once said to me:
"Alcatraz is the place to be"
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.java.programmer
csiph-web