Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 2: Learn Inheritance

17. Define a Generic Class

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 16. Implement an Interface Next episode: 18. Add Mixins

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

If you watched the previous course, you were introduced to generics. Generics allow us to write code that works on many different types. If you haven’t watched that episode, then stop watching now and watch that episode. It’ll get you up to speed.

class Adder<T extends num> {

}
T total;
Adder(this.total);
void add(T item) {
  total += total;
}
total = (total + item) as T;
var intAdder = Adder<int>(0);
intAdder.add(10);
intAdder.add(40);
print(intAdder.total);
var doubleAdder = Adder<double>(4.5);
doubleAdder.add(6.1);
doubleAdder.add(10.10);
print(doubleAdder.total);
var stringAdder = Adder<String>(0);