Skip to main content
An overridden getter is a mechanism in Dart inheritance that allows a subclass to provide a specific implementation for a property retrieval method originally declared in a superclass. By redefining the getter, the subclass intercepts read access to the property, replacing the superclass’s behavior with custom logic or derived values while strictly adhering to the interface contract defined by the parent.

Syntax

To override a getter, the subclass defines a getter method with the same name as the superclass property. The @override annotation is used to flag the intention to the compiler, ensuring static analysis checks the relationship between the parent and child members.
class SuperClass {
  // Explicit getter definition
  int get value => 10;
}

class SubClass extends SuperClass {
  // Overrides the parent getter
  @override
  int get value {
    return 20;
  }
}

void main() {
  SuperClass instance = SubClass();
  // Accesses the overridden implementation
  print(instance.value); // Output: 20
}

Technical Mechanics

Implicit and Explicit Overrides

Dart generates implicit getters for all instance variables (fields). Consequently, a subclass can override an explicit getter (defined with the get keyword) or an implicit getter (generated by a field declaration) using the same syntax. This allows a subclass to replace a stored memory field in the parent with a computed property in the child.
class Parent {
  // Generates an implicit getter for 'id'.
  // Must be final if the subclass only overrides the getter.
  final int id = 100; 
}

class Child extends Parent {
  // Overrides the implicit getter with a computed value
  @override
  int get id => 200; 
}

Mutability and Interface Compliance

When overriding a property, the subclass must satisfy the full interface implied by the superclass member. The mutability of the superclass property dictates the overriding requirements:
  • Final Fields/Getters: If the superclass member is final or defines only a getter, the subclass may override it with a getter or a final field.
  • Non-Final Fields: If the superclass member is mutable (non-final), it implicitly defines both a getter and a setter. A subclass cannot override only the getter for a non-final field; it must override both the getter and the setter to fulfill the interface contract. Failing to do so results in a compile-time error.

Return Type Covariance

The return type of the overridden getter must be covariant with the superclass definition. The return type in the subclass must be the same as, or a strict subtype of, the return type defined in the superclass. This guarantees type safety when a subclass instance is upcast to the superclass type.
class Base {
  num get metric => 10.5;
}

class Derived extends Base {
  @override
  double get metric => 20.5; // Valid: 'double' is a subtype of 'num'
}

class InvalidDerived extends Base {
  // @override
  // String get metric => "20"; // Error: 'String' is not a subtype of 'num'
}

Super Delegation

The super keyword provides access to the getter implementation of the immediate superclass. This allows the overriding getter to extend the existing logic by invoking the parent’s computation before applying additional transformations.
class Base {
  int get count => 5;
}

class Derived extends Base {
  @override
  int get count => super.count * 2; // Retrieves 5, returns 10
}
Master Dart with Deep Grasping Methodology!Learn More