Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a super constructor in Dart?

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

like image 935
Eduardo Copat Avatar asked Oct 18 '22 10:10

Eduardo Copat


People also ask

How do I call a super constructor?

To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.

How do you use super in darts?

Super Keyword in Dart: In Dart, super keyword is used to refer immediate parent class object. It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keyword calls that instance.

How do you call a constructor in flutter?

Factory Constructor in Dart/Flutter We can use the factory keyword for a constructor that return an object instead of creating a new instance. class Customer { String name; int age; String location; static final Customer origin = Customer("", 0, ""); // factory constructor factory Customer.

Is super class constructor automatically called?

With super(parameter list) , the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.


2 Answers

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);  
}

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.

like image 63
Eduardo Copat Avatar answered Nov 12 '22 23:11

Eduardo Copat


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");
    }
}
like image 34
Soban Arshad Avatar answered Nov 12 '22 23:11

Soban Arshad