Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extend a class with a default private constructor in Dart?

Tags:

dart

Suppose that we have a class a:

class A {
  A._();
}

Its default constructor is private: A._().

Is there any way to extends that class?

Problem

class B extends A {

}

This results in a compiler error:

The superclass 'A' doesn't have a zero argument constructor.

Trying to compose any constructor for B myself (B()) results in another error:

The superclass 'A' doesn't have an unnamed constructor.
like image 333
creativecreatorormaybenot Avatar asked Jun 26 '18 08:06

creativecreatorormaybenot


1 Answers

No, there is no way. This is an effective way to prevent extending.

What you still can do is implementing the class.

class B implements A {}

If the class also has a public non-factory constructor, you can still extend it by forwarding the constructor call to such a named constructor of the super class.

class B extends A {
  B() : super.other();
}
like image 59
Günter Zöchbauer Avatar answered Nov 01 '22 05:11

Günter Zöchbauer