0Pricing
Dart Academy · Lesson

Bounded Type Parameters With extends

Constrain what a generic can accept.

Bounded Type Parameters With extends is a free Dart Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Any Type Is Too Much

A plain type parameter accepts every type, but sometimes you need more. You may want to call a method that only certain types provide. 🔒

The Bound With extends

You can add a bound using extends. It says the type parameter must be a given type or a subtype of it.

class Cage<T extends Animal> {}

What a Bound Guarantees

Inside the class, Dart now trusts that T is at least an Animal. So you can safely use any member that Animal defines.

class Cage<T extends Animal> {
  void feed(T a) => a.eat();
}

Rejecting Bad Types

The compiler blocks any type argument that does not satisfy the bound. Passing an unrelated type is an error you see immediately.

Cage<String>();

A Numeric Bound

A common bound is num, the parent of int and double. It lets a generic do arithmetic while still accepting both number types.

T addOne<T extends num>(T x) => x + 1 as T;

Bounds on Methods

Bounds work on generic methods too, not just classes. Place extends right after the type parameter name in the method.

num biggest<T extends num>(List<T> xs) =>
    xs.reduce((a, b) => a > b ? a : b);

Default Bound Is Object

When you omit a bound, the implicit one is Object?. That is why an unbounded T only lets you use members every object has.

Comparable Bound

To sort or compare, bind to Comparable. Then you may call compareTo on the values inside your generic code.

T max<T extends Comparable<T>>(T a, T b) =>
    a.compareTo(b) >= 0 ? a : b;

Bounds Document Intent

A bound also tells readers what kinds of types belong here. The signature itself explains the contract your code expects.

Self-Referential Bounds

Notice that T can appear in its own bound, as in T extends Comparable of T. This pattern ties a type to operations on itself.

Keep Bounds Minimal

Use the loosest bound that still works. Over-constraining shuts out valid types, while no bound at all leaves useful methods unreachable.

Quick Check

What does a bound let you do inside the class?

Recap

You used extends to bound a type parameter, unlocking the bound type's methods and rejecting unrelated types. Keep bounds as loose as the job needs. ✅

Frequently asked questions

Is the “Bounded Type Parameters With extends” lesson free?

Yes — the full text of “Bounded Type Parameters With extends” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Bounded Type Parameters With extends”?

Constrain what a generic can accept. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bounded Type Parameters With extends” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Generics Beat dynamic
  2. Generic Classes and Methods
  3. Bounded Type Parameters With extends
  4. Generic Collections in Practice
← Back to Dart Academy