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.

Transcript: 17. Define a Generic Class

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.

You learned how to use generics with functions. Now it’s time to use generics with your class. When you define a generic method, you must define the type by using a single capital letter.

For a class, you move this to the class definition so that all methods can use it. As you know that single letter represents a type. We don’t know what type it is going to be until runtime, but you can still call Object methods on it like ToString since we know that every object is child of the root object.

We can actually constrain our generic class by using extends. For instance, if you defined a repository class, you can state that the type can only be related to that class. At which point, you gain all the properties and methods of repository since every item is guaranteed to be a repository.

We’re going to create a class that takes in numbers and automatically adds them a collective sum. This will be generic class that will only accept numbers. To get started, open up DartPad.dev.

Let’s create a class called Adder. This will add all the numbers added to it. Let’s make it generic and extend from the num class. This class is used to represent ints and floats.

class Adder<T extends num> {

}

Next we’re going to store all the values in a variable called total. This will be, of course, the same type as the Adder.

T total;

Notice that we used the T element type defined in the class. We can use it everywhere since it is now defined class wide. Now we’ll create a constructor. The caller will pass in a starting value.

Adder(this.total);

Now to create our add method. This will take in a item of type T which we’ll add to the total.

void add(T item) {
  total += total;
}

Unfortunately, we’re getting an error. Dart is getting confused with the types so it’s not allowing us to add them together. We solve this by doing the add, then casting it to the type using the as keyword.

total = (total + item) as T;

We’re basically saying to Dart, “don’t worry - this is the correct type”. The as keyword makes sure to make it the correct type. Mind you, can you do this with any type. Now let’s create our adder and add some numbers.

var intAdder = Adder<int>(0);
intAdder.add(10);
intAdder.add(40);
print(intAdder.total);

And we will do the same with doubles.

var doubleAdder = Adder<double>(4.5);
doubleAdder.add(6.1);
doubleAdder.add(10.10);
print(doubleAdder.total);

Now let’s create a String adder.

var stringAdder = Adder<String>(0);

You’ll see we get an error informing us that we can’t create an instance using the String since it doesn’t conform to num. Let’s comment it out.

Now run the program. Our adder works. We can use it ints, doubles, and any other subclass of the num type. Best yet, we don’t have to duplicate the code. Well done!