The @override Annotation
While Dart permits implicit overriding, the standard convention requires decorating the overriding member with the @override annotation. This metadata instructs the Dart analyzer to validate that the method explicitly overrides a member inherited from a superclass.
If the superclass method is renamed or removed, or if the signature does not match, the analyzer flags the annotated method as a compile-time error.
Signature Compatibility and Variance
For a method override to be valid, the overriding method’s signature must be compatible with the superclass method’s signature. Dart enforces strict type safety rules based on variance to satisfy the Liskov Substitution Principle.Return Type Covariance
The return type of the overriding method must be the same type or a subtype of the return type defined in the superclass.Parameter Type Contravariance
The parameter types of the overriding method must be the same type or a supertype of the corresponding parameters in the superclass. Widening the parameter type is allowed, but narrowing it (requiring a subtype) is forbidden by default because the subclass must accept any argument the superclass accepted.The covariant Keyword
To narrow a parameter type (override with a subtype) contrary to standard contravariance rules, the parameter must be marked with the covariant keyword. This disables the static analysis check for that parameter and enforces the type check at runtime. If an incorrect type is passed at runtime, a TypeError is thrown.
Accessing Superclass Implementations
An overridden method replaces the superclass method in the dispatch chain. To invoke the original logic defined in the superclass, the subclass uses thesuper keyword.
noSuchMethod
The noSuchMethod method allows a class to handle invocations of members that do not exist on the class. This is a form of dynamic overriding.
Because Dart is statically typed, the compiler will reject calls to undefined methods on a typed instance. To utilize noSuchMethod, the instance must be typed as dynamic, or the class must implement an interface (without providing concrete implementations) so the compiler allows the call, deferring resolution to runtime.
Master Dart with Deep Grasping Methodology!Learn More





