151

How do I call a super constructor in Dart? Is it possible to call named super constructors?

8 Answers 8

220

Yes, it is, the syntax is close to C#, here is an example with both default constructor and named constructor:

class Foo { Foo(int a, int b) { //Code of constructor } Foo.named(int c, int d) { //Code of named constructor } } class Bar extends Foo { Bar(int a, int b) : super(a, b); } class Baz extends Foo { Baz(int c, int d) : super.named(c, d); Baz.named(int c, int d) : super.named(c, d); } 

If you want to initialize instance variables in the subclass, the super() call must be last in an initializer list.

class CBar extends Foo { int c; CBar(int a, int b, int cParam) : c = cParam, super(a, b); } 

You can read about the motivation behind this super() call guideline on /r/dartlang.

Sign up to request clarification or add additional context in comments.

Comments

38

This is a file I am sharing with you, run it as is. You'll learn how to call super constructor, and how to call super parameterized constructor.

// Objectives // 1. Inheritance with Default Constructor and Parameterised Constructor // 2. Inheritance with Named Constructor void main() { var dog1 = Dog("Labrador", "Black"); var dog2 = Dog("Pug", "Brown"); var dog3 = Dog.myNamedConstructor("German Shepherd", "Black-Brown"); } class Animal { String color; Animal(String color) { this.color = color; print("Animal class constructor"); } Animal.myAnimalNamedConstrctor(String color) { print("Animal class named constructor"); } } class Dog extends Animal { String breed; Dog(String breed, String color) : super(color) { this.breed = breed; print("Dog class constructor"); } Dog.myNamedConstructor(String breed, String color) : super.myAnimalNamedConstrctor(color) { this.breed = breed; print("Dog class Named Constructor"); } } 

1 Comment

What version of dart did you use for this example, v2.19 gives an error Non-nullable field 'color' must be initialised
17

case of a constructor with Optional Parameters

class Foo { String a; int b; Foo({this.a, this.b}); } class Bar extends Foo { Bar({a,b}) : super(a:a, b:b); } 

2 Comments

In your constructor of Bar 'a' and 'b' parameters are 'dynamic' type. You missed advantages of statically typed language.
The dynamic type can be removed by class Bar extends Foo { Bar({required String a, required int b}) : super(a:a, b:b); }
10

Can I call a private constructor of the superclass?

Yes, but only if the superclass and the subclass you are creating are in the same library. (Since private identifiers are visible across the whole library they are defined in). Private identifiers are those that start with underscore.

class Foo { Foo._private(int a, int b) { //Code of private named constructor } } class Bar extends Foo { Bar(int a, int b) : super._private(a,b); } 

Comments

4

As dart supports implementing a class as interface (Implicit interfaces), you can't call the parent constructor if you implemented it you should use extends. If you use implements change it to extends and use Eduardo Copat's Solution.

Comments

4
class AppException implements Exception { final _message; final _prefix; //constructor with optional & not named paramters //if not the parameter is not sent, it'll be null AppException([this._message, this._prefix]); String toString() { return "$_prefix$_message"; } } class FetchDataException extends AppException { //redirecting constructor with a colon to call parent two optional parameters FetchDataException([String msg]) : super(msg, "Error During Communication: "); } class BadRequestException extends AppException { BadRequestException([msg]) : super(msg, "Invalid Request: "); } class UnauthorisedException extends AppException { UnauthorisedException([msg]) : super(msg, "Unauthorised: "); } class InvalidInputException extends AppException { InvalidInputException([String msg]) : super(msg, "Invalid Input: "); } 

Comments

3

A cool addition to the previous answers is that dart (from version 2.17 on) allows passing arguments to the super constructor without explicitly calling super(), as in

void main() { print(Bar(42).a); } class Foo { int a; Foo(this.a); } class Bar extends Foo{ Bar(super.a); } 

and the same works with named arguments.

Comments

0
void main() { var dog1 = Dog("Labrador", "Black"); var dog2 = Dog("Pug", "Brown"); var dog3 = Dog.myNamedConstructor("German Shepherd", "Black-Brown"); /* output Animal class constructor Dog class constructor Animal class constructor Dog class constructor Animal class named constructor Dog class Named Constructor */ } class Animal { final String color; Animal(this.color) { print("Animal class constructor"); } Animal.myAnimalNamedConstrctor(this.color) { print("Animal class named constructor"); } } class Dog extends Animal { final String breed; final String color; Dog(this.breed, this.color) : super(color) { print("Dog class constructor"); } Dog.myNamedConstructor(this.breed, this.color) : super.myAnimalNamedConstrctor(color) { print("Dog class Named Constructor"); } } class Cat extends Animal { final String breed; Cat(this.breed, super.color){ print("Cat class constructor"); } Cat.myNamedConstructor(this.breed, super.color) : super.myAnimalNamedConstrctor() { print("Cat class Named Constructor"); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.