@@ -94,143 +94,6 @@ A subclass doesn't inherit a superclass's named constructor.
94
94
To create a subclass with a named constructor defined in the superclass,
95
95
implement that constructor in the subclass.
96
96
97
- ### Non-default superclass constructors
98
-
99
- Dart executes constructors in the following order:
100
-
101
- 1 . [ initializer list] ( #use-an-initializer-list )
102
- 1 . superclass's unnamed, no-arg constructor
103
- 1 . main class's no-arg constructor
104
-
105
- If the superclass lacks an unnamed, no-argument constructor,
106
- call one of the constructors in the superclass.
107
- Before the constructor body (if any),
108
- specify the superclass constructor after a colon (` : ` ).
109
-
110
- In the following example,
111
- the ` Employee ` class constructor calls the named constructor
112
- for its superclass, ` Person ` . To execute the following code, click ** Run** .
113
-
114
- <? code-excerpt "employee.dart (super)" plaster="none"?>
115
- ``` dart:run-dartpad:height-450px:ga_id-non_default_superclass_constructor
116
- class Person {
117
- String? firstName;
118
-
119
- Person.fromJson(Map data) {
120
- print('in Person');
121
- }
122
- }
123
-
124
- class Employee extends Person {
125
- // Person does not have a default constructor;
126
- // you must call super.fromJson().
127
- Employee.fromJson(super.data) : super.fromJson() {
128
- print('in Employee');
129
- }
130
- }
131
-
132
- void main() {
133
- var employee = Employee.fromJson({});
134
- print(employee);
135
- // Prints:
136
- // in Person
137
- // in Employee
138
- // Instance of 'Employee'
139
- }
140
- ```
141
-
142
- As Dart evaluates the arguments to the superclass constructor * before*
143
- invoking the constructor, an argument can be an expression like a
144
- function call.
145
-
146
- <? code-excerpt "employee.dart (method-then-constructor)"?>
147
- ``` dart
148
- class Employee extends Person {
149
- Employee() : super.fromJson(fetchDefaultData());
150
- // ···
151
- }
152
- ```
153
-
154
- ::: warning
155
- Arguments to the superclass constructor can't access ` this ` .
156
- For example, arguments can call * static* methods
157
- but not * instance* methods.
158
- :::
159
-
160
- #### Super parameters
161
-
162
- To avoid passing each parameter into the super invocation of a constructor,
163
- use super-initializer parameters to forward parameters
164
- to the specified or default superclass constructor.
165
- You can't use this feature with
166
- [ redirecting constructors] ( #redirecting-constructors ) .
167
- Super-initializer parameters have syntax and semantics like
168
- [ initializing formal parameters] ( #use-initializing-formal-parameters ) .
169
-
170
- ::: version-note
171
- Using super-initializer parameters
172
- requires a [ language version] [ ] of at least 2.17.
173
- If you're using an earlier language version,
174
- you must manually pass in all super constructor parameters.
175
- :::
176
-
177
- If the super-constructor invocation includes positional arguments,
178
- super-initializer parameters can't be positional.
179
-
180
- <? code-excerpt "super_initializer_positional_parameters.dart (positional)" plaster="none"?>
181
- ``` dart
182
- class Vector2d {
183
- final double x;
184
- final double y;
185
-
186
- Vector2d(this.x, this.y);
187
- }
188
-
189
- class Vector3d extends Vector2d {
190
- final double z;
191
-
192
- // Forward the x and y parameters to the default super constructor like:
193
- // Vector3d(final double x, final double y, this.z) : super(x, y);
194
- Vector3d(super.x, super.y, this.z);
195
- }
196
- ```
197
-
198
- To further illustrate, consider the following example.
199
-
200
- ``` dart
201
- // If you invoke the super constructor (`super(0)`) with any
202
- // positional arguments, using a super parameter (`super.x`)
203
- // results in an error.
204
- Vector3d.xAxisError(super.x): z = 0, super(0); // BAD
205
- ```
206
-
207
- This named constructor tries to set the ` x ` value twice:
208
- once in the super constructor and once as a
209
- positional super parameter.
210
- As both address the ` x ` positional parameter, this results in an error.
211
-
212
- When the super constructor has named arguments, you can split them
213
- between named super parameters (` super.y ` in the next example)
214
- and named arguments to the super constructor invocation
215
- (` super.named(x: 0) ` ).
216
-
217
- <? code-excerpt "super_initializer_named_parameters.dart (named)" plaster="none"?>
218
- ``` dart
219
- class Vector2d {
220
- // ...
221
- Vector2d.named({required this.x, required this.y});
222
- }
223
-
224
- class Vector3d extends Vector2d {
225
- final double z;
226
-
227
- // Forward the y parameter to the named super constructor like:
228
- // Vector3d.yzPlane({required double y, required this.z})
229
- // : super.named(x: 0, y: y);
230
- Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);
231
- }
232
- ```
233
-
234
97
### Constant constructors
235
98
236
99
If your class produces unchanging objects, make these
@@ -359,22 +222,6 @@ Redirecting factories have several advantages:
359
222
* A redirecting factory constructor avoids the need for forwarders
360
223
to repeat the formal parameters and their default values.
361
224
362
- ## Constructor inheritance
363
-
364
- _ Subclasses_ , or child classes, don't inherit * constructors*
365
- from their _ superclass_ , or immediate parent class.
366
- If a class doesn't declare a constructor, it can only use the
367
- [ default constructor] ( #default-constructors ) .
368
-
369
- A class can inherit the _ parameters_ of a superclass.
370
- These are called [ super parameters] ( #super-parameters )
371
-
372
- Constructors work in a somewhat similar way to
373
- how you call a chain of static methods.
374
- Each subclass can call its superclass's constructor to initialize an instance,
375
- like a subclass can call a superclass's static method.
376
- This process doesn't "inherit" constructor bodies or signatures.
377
-
378
225
## Instance Variable Initialization
379
226
380
227
Dart can initialize variables in three ways.
@@ -444,6 +291,7 @@ Private fields can't be used as named initializing formals.
444
291
Don't attach the following example to a code excerpt.
445
292
It doesn't work on purpose and will cause errors in CI.
446
293
{% endcomment %}
294
+
447
295
``` dart
448
296
class PointB {
449
297
// ...
@@ -560,6 +408,159 @@ void main() {
560
408
}
561
409
```
562
410
411
+ ## Constructor inheritance
412
+
413
+ _ Subclasses_ , or child classes, don't inherit * constructors*
414
+ from their _ superclass_ , or immediate parent class.
415
+ If a class doesn't declare a constructor, it can only use the
416
+ [ default constructor] ( #default-constructors ) .
417
+
418
+ A class can inherit the _ parameters_ of a superclass.
419
+ These are called [ super parameters] ( #super-parameters )
420
+
421
+ Constructors work in a somewhat similar way to
422
+ how you call a chain of static methods.
423
+ Each subclass can call its superclass's constructor to initialize an instance,
424
+ like a subclass can call a superclass's static method.
425
+ This process doesn't "inherit" constructor bodies or signatures.
426
+
427
+ ### Non-default superclass constructors
428
+
429
+ Dart executes constructors in the following order:
430
+
431
+ 1 . [ initializer list] ( #use-an-initializer-list )
432
+ 1 . superclass's unnamed, no-arg constructor
433
+ 1 . main class's no-arg constructor
434
+
435
+ If the superclass lacks an unnamed, no-argument constructor,
436
+ call one of the constructors in the superclass.
437
+ Before the constructor body (if any),
438
+ specify the superclass constructor after a colon (` : ` ).
439
+
440
+ In the following example,
441
+ the ` Employee ` class constructor calls the named constructor
442
+ for its superclass, ` Person ` . To execute the following code, click ** Run** .
443
+
444
+ <? code-excerpt "employee.dart (super)" plaster="none"?>
445
+ ``` dart:run-dartpad:height-450px:ga_id-non_default_superclass_constructor
446
+ class Person {
447
+ String? firstName;
448
+
449
+ Person.fromJson(Map data) {
450
+ print('in Person');
451
+ }
452
+ }
453
+
454
+ class Employee extends Person {
455
+ // Person does not have a default constructor;
456
+ // you must call super.fromJson().
457
+ Employee.fromJson(super.data) : super.fromJson() {
458
+ print('in Employee');
459
+ }
460
+ }
461
+
462
+ void main() {
463
+ var employee = Employee.fromJson({});
464
+ print(employee);
465
+ // Prints:
466
+ // in Person
467
+ // in Employee
468
+ // Instance of 'Employee'
469
+ }
470
+ ```
471
+
472
+ As Dart evaluates the arguments to the superclass constructor * before*
473
+ invoking the constructor, an argument can be an expression like a
474
+ function call.
475
+
476
+ <? code-excerpt "employee.dart (method-then-constructor)"?>
477
+ ``` dart
478
+ class Employee extends Person {
479
+ Employee() : super.fromJson(fetchDefaultData());
480
+ // ···
481
+ }
482
+ ```
483
+
484
+ ::: warning
485
+ Arguments to the superclass constructor can't access ` this ` .
486
+ For example, arguments can call * static* methods
487
+ but not * instance* methods.
488
+ :::
489
+
490
+ ### Super parameters
491
+
492
+ To avoid passing each parameter into the super invocation of a constructor,
493
+ use super-initializer parameters to forward parameters
494
+ to the specified or default superclass constructor.
495
+ You can't use this feature with
496
+ [ redirecting constructors] ( #redirecting-constructors ) .
497
+ Super-initializer parameters have syntax and semantics like
498
+ [ initializing formal parameters] ( #use-initializing-formal-parameters ) .
499
+
500
+ ::: version-note
501
+ Using super-initializer parameters
502
+ requires a [ language version] [ ] of at least 2.17.
503
+ If you're using an earlier language version,
504
+ you must manually pass in all super constructor parameters.
505
+ :::
506
+
507
+ If the super-constructor invocation includes positional arguments,
508
+ super-initializer parameters can't be positional.
509
+
510
+ <? code-excerpt "super_initializer_positional_parameters.dart (positional)" plaster="none"?>
511
+ ``` dart
512
+ class Vector2d {
513
+ final double x;
514
+ final double y;
515
+
516
+ Vector2d(this.x, this.y);
517
+ }
518
+
519
+ class Vector3d extends Vector2d {
520
+ final double z;
521
+
522
+ // Forward the x and y parameters to the default super constructor like:
523
+ // Vector3d(final double x, final double y, this.z) : super(x, y);
524
+ Vector3d(super.x, super.y, this.z);
525
+ }
526
+ ```
527
+
528
+ To further illustrate, consider the following example.
529
+
530
+ ``` dart
531
+ // If you invoke the super constructor (`super(0)`) with any
532
+ // positional arguments, using a super parameter (`super.x`)
533
+ // results in an error.
534
+ Vector3d.xAxisError(super.x): z = 0, super(0); // BAD
535
+ ```
536
+
537
+ This named constructor tries to set the ` x ` value twice:
538
+ once in the super constructor and once as a
539
+ positional super parameter.
540
+ As both address the ` x ` positional parameter, this results in an error.
541
+
542
+ When the super constructor has named arguments, you can split them
543
+ between named super parameters (` super.y ` in the next example)
544
+ and named arguments to the super constructor invocation
545
+ (` super.named(x: 0) ` ).
546
+
547
+ <? code-excerpt "super_initializer_named_parameters.dart (named)" plaster="none"?>
548
+ ``` dart
549
+ class Vector2d {
550
+ // ...
551
+ Vector2d.named({required this.x, required this.y});
552
+ }
553
+
554
+ class Vector3d extends Vector2d {
555
+ final double z;
556
+
557
+ // Forward the y parameter to the named super constructor like:
558
+ // Vector3d.yzPlane({required double y, required this.z})
559
+ // : super.named(x: 0, y: y);
560
+ Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);
561
+ }
562
+ ```
563
+
563
564
[ language version ] : /guides/language/evolution#language-versioning
564
565
[ using constructors ] : /language/classes#using-constructors
565
566
[ late-final-ivar ] : /effective-dart/design#avoid-public-late-final-fields-without-initializers
0 commit comments