Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dart support multiple inheritance?

Tags:

dart

What are the mechanisms for multiple inheritance supported by Dart?

like image 315
pamphlet Avatar asked Jul 18 '26 05:07

pamphlet


1 Answers

No, Dart does not support multiple implementation inheritance.

Dart has interfaces, and like most other similar languages it has multiple interface inheritance.

For implementation, there is only a single super-class chain that a class can inherit member implementations from.

Dart does have mixins, which allows implementation to be used by multiple classes, but not through inheritance as much as by mixin application.

Example:

class A {
  String get foo;
}
class A1 implements A {
  String get foo => "A1";
}
class A2 implements A {
  String get foo => "A2";
}
mixin B on A {
  String get foo => "B:${super.foo}";
}
class C extends A1 with B {
  String get foo => "C:${super.foo}";
}
class D extends A2 with B {
  String get foo => "D:${super.foo}";
}
void main() {
  print(C().foo); // C:B:A1
  print(D().foo); // D:B:A2
}

Here the same member, B.foo, is mixed into two different classes, with two different super-classes.

Each of the classes C and D has only a single superclass chain. The superclass of C is the anonymous mixin application class A1 with B, the superclass of D is another anonymous mixin application class A2 with B. Both of these classes contain the mixin member B.foo.

Mixins are not multiple inheritance, but it's the closest you'll get in Dart.

like image 139
lrn Avatar answered Jul 19 '26 22:07

lrn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!