mustBeOverridden top-level constant

_MustBeOverridden const mustBeOverridden

Used to annotate an instance member m declared on a class or mixin C. Indicates that every concrete subclass of C must directly override m.

The intention of this annotation is to "re-abtract" a member that was previously concrete, and to ensure that subclasses provide their own implementation of the member. For example:

base class Entity {
  @mustBeOverridden
  String toString();
}

abstract class AbstractEntity extends Entity {
  // OK: AbstractEntity is abstract.
}

sealed class SealedEntity extends Entity {
  // OK: SealedEntity is sealed, which implies abstract.
}

mixin MixinEntity on Entity {
 // OK: MixinEntity is abstract.
}

class Person extends Entity {
  // ERROR: Missing new implementation of 'toString'.
}

class Animal extends Entity {
  // OK: Animal provides its own implementation of 'toString'.
  String toString() => 'Animal';
}

This annotation places no restrictions on the overriding members. In particular, it does not require that the overriding members invoke the overridden member. The annotation mustCallSuper can be used to add that requirement.

Tools, such as the analyzer, can provide feedback if

  • the annotation is associated with anything other than an instance member (a method, operator, field, getter, or setter) of a class or of a mixin, or
  • the annotation is associated with a member m in class or mixin C, and there is a concrete class D which is a subclass of C (directly or indirectly), and D does not directly declare a concrete override of m and does not directly declare a concrete override of noSuchMethod.

Implementation

const _MustBeOverridden mustBeOverridden = _MustBeOverridden();