Skip to main content

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.

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.

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

class BaseClass {
  void processData(String data) {
    print("Base processing: $data");
  }
}

class DerivedClass extends BaseClass {
  @override
  void processData(String data) {
    print("Derived processing: $data");
  }
}

Invoking the Superclass Method

An overridden method can access the original implementation of the superclass using the super keyword. This is necessary when the subclass intends to augment rather than completely replace the parent method’s execution context.
class DerivedClass extends BaseClass {
  @override
  void processData(String data) {
    // Invokes the overridden method in the parent class
    super.processData(data); 
    print("Additional derived processing: $data");
  }
}

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:
  1. 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.
  2. 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.
  3. Covariant Parameters (with covariant keyword): While standard parameters are contravariant, Dart allows tightening parameter types to a subtype (covariance) in an overridden method if the covariant keyword 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.
class Animal {}
class Dog extends Animal {}

class BaseProcessor {
  Animal generate() => Animal();
  void consume(Dog dog) {}
  void process(Animal animal) {}
}

class StrictProcessor extends BaseProcessor {
  // Valid: Return type is covariant (Dog is a subtype of Animal)
  @override
  Dog generate() => Dog();

  // Valid: Parameter type is contravariant (Animal is a supertype of Dog)
  @override
  void consume(Animal animal) {}

  // Valid: Parameter type is covariant using the covariant keyword.
  // Also valid: Adding a new optional parameter.
  @override
  void process(covariant Dog animal, [String? extraData]) {}
}

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