An overridden method in Dart is an instance method within a subclass that provides a new implementation for a method inherited from its superclass. It allows a subclass to replace or extend the inherited behavior while adhering to the method signature constraints defined by the parent class, enabling runtime polymorphism through dynamic dispatch.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
The @override Annotation
Dart utilizes the @override metadata annotation to explicitly indicate that a method is intended to override a superclass method. While technically optional, it is a critical compiler directive. If the annotated method fails to correctly match a superclass method signature, the Dart analyzer throws a compile-time error, preventing silent inheritance failures.
Syntax and Implementation
Invoking the Superclass Method
An overridden method can access the original implementation of the superclass using thesuper keyword. This is necessary when the subclass intends to augment rather than completely replace the parent method’s execution context.
Technical Constraints and Signature Rules
To successfully override a method in Dart, the overriding method must adhere to strict type system rules regarding its signature:- Parameter Matching and Contravariance: The overriding method must accept all required parameters of the superclass method. It may introduce new optional parameters (either positional or named). Furthermore, standard parameter types are contravariant; the overriding method can declare a parameter as a supertype of the corresponding parameter in the superclass.
- Covariant Return Types: Dart supports return type covariance. The return type of the overriding method can be the same type or a subtype of the return type declared in the superclass method.
- Covariant Parameters (with
covariantkeyword): While standard parameters are contravariant, Dart allows tightening parameter types to a subtype (covariance) in an overridden method if thecovariantkeyword is explicitly applied to the parameter in either the superclass or the subclass. This bypasses standard static type checking for that parameter in favor of runtime checks.
Restrictions
- Static Methods: Static methods belong to the class itself, not the instance, and therefore cannot be overridden. They are resolved at compile-time via static dispatch.
- Constructors: Constructors are not inherited and cannot be overridden.
- Private Methods: Methods prefixed with an underscore (
_) are library-private. They can only be overridden if the subclass resides within the same library (file) as the superclass.
Master Dart with Deep Grasping Methodology!Learn More





