---
title: "Dart Inheritance and Super Calls: Building Type Hierarchies with extends and super"
date: "2026-12-04T08:52:13+07:00"
description: "A step-by-step guide to Dart single inheritance: extends, super method calls, super parameters, override rules, covariant, and noSuchMethod."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-inheritance-super-calls.jpg"
alt: "Diagram of a Dart class hierarchy showing a superclass and subclass connected by extends and super calls"
tags: ['dart', 'dart-language', 'inheritance', 'super-calls', 'subclasses']
---

Modeling a domain usually starts with a family of related things: a `Spacecraft` and an `Orbiter`, a `Vehicle` and a `Car`. When the child type genuinely *is* a parent type, duplicating the shared fields and methods is the wrong move — and patching the copy-pasted version later is worse.

Dart gives you single inheritance: every class (except `Object?`) has exactly one superclass, and a subclass can reuse, override, and extend what the superclass provides. The two keywords that drive the whole mechanism are `extends` (create the subclass) and `super` (reach back to the parent).

This post walks through the mechanics step by step: creating subclasses, overriding members correctly, calling superclass constructors with `super` and super parameters, and the rules the analyzer enforces when signatures don't quite line up.

## 1. Creating a subclass with `extends`

Use `extends` to create a subclass, and `super` to refer to the superclass:

```dart
class Television {
  void turnOn() {
    _illuminateDisplay();
    _activateIrSensor();
  }

  void _illuminateDisplay() {}
  void _activateIrSensor() {}
}

class SmartTelevision extends Television {
  @override
  void turnOn() {
    super.turnOn(); // Run the parent's logic first...
    _bootNetworkInterface(); // ...then layer on subclass behavior.
    _initializeMemory();
    _upgradeApps();
  }

  void _bootNetworkInterface() {}
  void _initializeMemory() {}
  void _upgradeApps() {}
}
```

`super.turnOn()` invokes the superclass implementation directly. That's the "call the original, then add more" pattern — the same shape as `super()` in most other languages. Subclasses also inherit everything they don't override, and can forward constructor arguments to the parent with super parameters — assuming a `Spacecraft(String name, DateTime launchDate)` constructor:

```dart
class Orbiter extends Spacecraft {
  double altitude;

  Orbiter(super.name, DateTime super.launchDate, this.altitude);
  //         ^^^^^^^^          ^^^^^^^^^^^^^^
  //         passed to Spacecraft's constructor automatically
}
```

If you're coming from a class with initializing formal constructors, see [constructors and initializing formals](/posts/dart-classes-constructors-initializing-formals/) for how `this.name` and `super.name` differ.

## 2. Overriding members with `@override`

Subclasses can override instance methods (including operators), getters, and setters. The `@override` annotation is optional but recommended — it tells readers and the analyzer that the override is intentional:

```dart
class Television {
  set contrast(int value) {
    // ...
  }
}

class SmartTelevision extends Television {
  @override
  set contrast(num value) {
    // Widened the parameter type from int to its supertype, num.
  }
}
```

Without `@override`, a typo in the member name silently creates a brand-new member instead of overriding the old one. With it, the analyzer complains if the parent doesn't actually have that member.

### Instance variables are getters and setters too

Every instance variable generates an implicit getter, and non-final variables generate an implicit setter — so both can be overridden the same way methods can. The [getters and setters post](/posts/dart-getters-setters/) covers the underlying mechanics.

## 3. The override signature rules

An overriding declaration must match the overridden member in several ways:

| Aspect | Rule | Example |
| --- | --- | --- |
| Return type | Must be the same type **or a subtype** of the overridden return type | `Vehicle moveForward()` may be overridden by `Car moveForward()` |
| Parameter types | Must be the same type **or a supertype** of the overridden parameter types | `set contrast(int)` overridden by `set contrast(num)` |
| Positional parameter count | If the overridden method accepts *n* positional parameters, the override must too | Dropping a positional parameter is a compile error |
| Generic methods | A generic method can't override a non-generic one, and vice versa | `T f<T>()` can't override `void f()` |

The asymmetry is deliberate: return types are *covariant* (you can narrow them) and parameters are *contravariant* (you can widen them), which keeps assignment safe in both directions.

```dart
class Vehicle {
  void moveForward(int meters) {}
}

class Car extends Vehicle {
  @override
  void moveForward(int meters) {} // OK: same signature.

  // Error: return type 'int' isn't a subtype of 'void'... and
  // Error: missing a positional parameter the parent declares.
  int moveForward() => 0;
}
```

### Narrowing a parameter with `covariant`

Sometimes you *want* to narrow a parameter type — a classic case is a subclass that only accepts its own specialized argument. This violates the normal rules, so Dart requires the `covariant` keyword, which pushes the type check to runtime:

```dart
class Vehicle {
  void tow(Vehicle other) {}
}

class Car extends Vehicle {
  @override
  void tow(covariant Car other) { // Narrowed: runtime-checked.
    // ...
  }
}
```

If a non-`Car` is passed to a `Car.tow`, the narrowed parameter throws a `TypeError` at runtime — similar to a downcast.

## 4. Calling superclass constructors

Dart constructors don't inherit automatically. A subclass constructor must explicitly invoke the superclass constructor, either through an initializer list or a `super.` parameter:

```dart
class Orbiter extends Spacecraft {
  double altitude;

  // Initializer-list style:
  Orbiter(String name, DateTime launchDate, this.altitude)
      : super(name, launchDate);

  // Super-parameter style (preferred when arguments map 1:1):
  // Orbiter(super.name, DateTime super.launchDate, this.altitude);
}
```

If the superclass has only a default constructor, the subclass doesn't need to write anything — it's called implicitly. But a superclass constructor with required arguments forces the subclass to forward them somehow.

> Tip: when a subclass constructor's parameters line up with the parent's, super parameters (`super.name`) are shorter and less error-prone than an initializer list. The dedicated [super parameters post](/posts/dart-super-parameters/) goes deeper.

## 5. What you can and can't override

A quick map of the override surface:

| Member kind | Overridable? | Notes |
| --- | --- | --- |
| Instance method | Yes | Follow the return/parameter rules above |
| Operator (e.g. `==`, `+`) | Yes | Same rules apply |
| Getter / setter | Yes | Implicitly generated from fields |
| Static member | No | Statics are resolved on the class, not instances |
| Constructor | No | Constructors aren't inherited; they're forwarded |
| Private (`_name`) member | Same library only | A `_`-prefixed member is invisible across libraries, so it can't be overridden externally |

One important warning from the docs: if you override `==`, you should also override `Object`'s `hashCode` getter. Otherwise objects used as map keys or in sets can behave inconsistently.

## 6. Reacting to missing members with `noSuchMethod()`

Normally, calling a member that doesn't exist throws `NoSuchMethodError`. You can intercept that by overriding `noSuchMethod()`:

```dart
class A {
  @override
  void noSuchMethod(Invocation invocation) {
    print('Missing member: ${invocation.memberName}');
  }
}
```

You **can't invoke** an unimplemented method unless one of the following is true:

- The receiver has the static type `dynamic`.
- The receiver's static type defines the unimplemented method (abstract is fine), and the receiver's dynamic type has an `noSuchMethod()` implementation different from `Object`'s.

This is the escape hatch behind mock frameworks: declare an abstract class, implement members only in tests, and route everything else through `noSuchMethod`.

## 7. Inheritance vs. the alternatives

| Need | Use |
| --- | --- |
| Share implementation down a true *is-a* hierarchy | `extends` |
| Reuse a class API without its implementation | `implements` (covered in [abstract classes and interfaces](/posts/dart-abstract-classes-interfaces/)) |
| Reuse a behavior bundle across unrelated hierarchies | `with` mixins ([mixins post](/posts/dart-mixins/)) |
| Add a method to a type you don't own | `extension` ([extensions post](/posts/dart-extensions/)) |
| Keep subtypes out of a hierarchy entirely | `base`, `final`, `sealed` modifiers |

Dart's class system is mixin-based: each class has exactly one superclass, but class *bodies* can be reused across many hierarchies. Inheritance is the first tool you reach for — not the only one.

## Checklist

- [ ] Use `extends` only when the subclass is genuinely a subtype of the parent (`is-a`, not just *has-a*).
- [ ] Annotate every intentional override with `@override` so typos surface as analyzer errors.
- [ ] Narrow return types freely in overrides; widen parameter types instead of narrowing them.
- [ ] When you must narrow a parameter, mark it `covariant` and be ready for runtime `TypeError`s.
- [ ] Forward superclass constructor arguments explicitly — super parameters are terser than initializer lists.
- [ ] Keep positional parameter counts identical between parent and override.
- [ ] Override `hashCode` whenever you override `==`.
- [ ] Don't try to override static members or constructors; forward constructors instead.
- [ ] Reach for `noSuchMethod()` only for deliberate dynamic dispatch (mocks, proxies), not everyday code.

## Conclusion

Single inheritance in Dart is deliberately small: `extends` builds the hierarchy, `super` reaches back up it, and a handful of signature rules keep overrides type-safe. Once you internalize the covariance rules — narrow returns, widen parameters — most override errors become obvious at a glance.

But inheritance is only one axis of reuse. When classes don't share a lineage, mixins and interfaces pick up where `extends` stops, and class modifiers decide who outside your library is allowed to subtype at all. The next posts in this series cover those contracts in detail.

## References

- https://dart.dev/language
- https://dart.dev/language/extend
- https://dart.dev/language/classes
- https://dart.dev/language/class-modifiers
