Calling the parent constructor

631 views
Skip to first unread message

Ad van der Veer

unread,
Oct 11, 2011, 3:44:50 AM10/11/11
to General Dart Discussion
Heya,

im already getting quiet obsessed by this new language and really
enjoy writing it.

i was wondering how you can call the parent constructor inside the
function body of
a child class.

Example ( below could probably be achieved in a better way.):


class Human {

String name;

Human(String name) {
this.name = name + '!';
}

}


class Child extends Human {

Child(String name) {

//parent constructor call because i want the name to
initialise with a '!'
...

//then do local construction to add a question mark?
this.name = this.name + '?';

}

}

Steve Messick

unread,
Oct 11, 2011, 4:26:15 AM10/11/11
to Ad van der Veer, General Dart Discussion
Glad to hear you like it!

To invoke the superclass constructor, just use an initializer in the
constructor definition.

class Child {
Child(String name) : super(name) {
...

Zexx

unread,
Oct 11, 2011, 5:13:52 AM10/11/11
to General Dart Discussion
Do you call the parent method of an non-constructor method the same
way? What if you want to call the parent method *after* executing the
code of your inherited method? Can it be done?

Ad van der Veer

unread,
Oct 11, 2011, 5:43:20 AM10/11/11
to General Dart Discussion
Yes, thats what i was wondering. Basicly im expecting something php
like:


parent.__construct(name)

CB Ash

unread,
Oct 11, 2011, 9:17:05 AM10/11/11
to General Dart Discussion
Following on this, would issuing a call to a method in the parent from
the child extending from said parent be along these lines for Dart?

class Child extends Human {
Learning(_myval) {
_value_learned = Child:Human.TeachSomething(_myval);

Gilad Bracha

unread,
Oct 11, 2011, 11:27:03 AM10/11/11
to CB Ash, General Dart Discussion

Just plain super.teachSomething(_myval) will do. 
--
Cheers, Gilad

Ad van der Veer

unread,
Oct 11, 2011, 6:49:24 PM10/11/11
to General Dart Discussion
Hey Gilard, could you eleborate a bit more. I am
familiar with the super.method syntax but it seems i cant
use this to call a parent constructor. Should i file a
feature request?

Gilad Bracha

unread,
Oct 11, 2011, 7:34:44 PM10/11/11
to Ad van der Veer, General Dart Discussion
Hi,

All accessible instance instance members of all kinds can be accessed via super in the traditional ways.

Constructors are different. They are not instance members. You don't call a super constructor the same way.

This is because the construction process is carefully constrained, so that one can never see final instance variables in an uninitialized state (and if one wants, one can also initialize all other instance variables without exposing their uninitialized state).

Object construction consists of several phases. Simplifying slightly, field initialization happens first, via an initializer list (written after the signature) and the constructor bodies are executed afterwards. You call the superconstructors initializer list during the first phase via the syntax

Foo(a): x = 3, super(a) {}


The body implicitly calls the super body before it starts.

It is all in the spec.  I agree we need a tutorial for this soon.

--
Cheers, Gilad

Ad van der Veer

unread,
Oct 12, 2011, 4:51:33 AM10/12/11
to General Dart Discussion
Hey Gilad,

thanks for the explanation i must say that the specs are quiet
difficult
for me to understand but you are basicly stating that:

1. If you want to let the parent class let it run its constructor this
should
be done before the child constructor: Foo(a): super(a) { .... }

2. Because of the phases of object construction and the constrins it
gives us i cannot call the parent constructor halfway during the child
constructor (because the constructor ain a instance member)

Which means that if i want to call constructor logic of the parent
from
the child i should put this parent logic into another instance member
of the parent
class.

So in general (maybe obvious): the constructor of a object should
limit itself
as much as possible to the initialization (assigning) of its
properties and any
(excess) logic surrounding this should best be moved to another method
(in te parent class) so it can be
called as an instance member from the child constructor?

On Oct 12, 1:34 am, Gilad Bracha <gbra...@google.com> wrote:
> Hi,
>
> All accessible instance instance members of all kinds can be accessed via
> super in the traditional ways.
>
> Constructors are different. They are not instance members. You don't call a
> super constructor the same way.
>
> This is because the construction process is carefully constrained, so that
> one can never see final instance variables in an uninitialized state (and if
> one wants, one can also initialize all other instance variables without
> exposing their uninitialized state).
>
> Object construction consists of several phases. Simplifying slightly, field
> initialization happens first, via an initializer list (written after the
> signature) and the constructor bodies are executed afterwards. You call the
> superconstructors initializer list during the first phase via the syntax
>
> Foo(a): x = 3, super(a) {}
>
> The body implicitly calls the super body before it starts.
>
> It is all in the spec.  I agree we need a tutorial for this soon.
>
Reply all
Reply to author
Forward
0 new messages