Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 2: Learn Inheritance

14. Challenge: Override a Method

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: 13. Override Methods Next episode: 15. Use Abstract Classes

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: 14. Challenge: Override a Method

So now you have a good idea about subclasses and then how to override methods, it’s time to put your newfound skills to the test. I want you to create a Profile class. It should contain a user name which is a string, a user id which is an int, and interests with is a list of strings.

Once you have your new class defined, I want you to override the toString method to print out all the properties. ToString is defined in the root object that allows is meant to provide a string representation of your object. Since it is a method defined in the root object, you need to override it in your Profile class.

Once you define the Profile class, create a new profile. Give it the name Larry, set the userId to 100, and finally, set his interests to swimming, dancing and coding. Finally print out the profile. Pause the video now and give it a shot.

How was that challenge for you? As always, if you got stuck, feel free to follow along with me. We’ll get started with a new DartPad instance up and running. First lets define our new Profile.

class Profile {

}

Now lets define some properties. We’re going to define a username, userid and interests.

int userId;
String username;
List<String> interests;

After which, we’ll define a constructor that will only accept the username.

Profile(this.username) : userId = 0, interests = [];

Okay, now to override the toString method. Remember, when we override a method, we need to annotate the method with the @override annotation. If you don’t provide this annotation, the compiler will let you know with a compile error.

@override

Next, we’ll add the toString method. We’ll use arrow notation to just return a string.

String toString() => 'userId: $userId, username: $username, interests: $interests';

And that’s it. Now let’s define our profile. In main, let’s create a new profile with the username, “Larry”.

var profile = Profile('Larry');

Let’s add a user id.

profile.userId = 100;

Finally, lets add the interests to the profile.

profile.interests.add('swimming');
profile.interests.add('dancing');
profile.interests.add('coding');

Okay, our profile is all setup. Now we just print it out.

print(profile);

Run the program. And look at that, we get our profile printed out. For curiosity sake, let’s move the toString method. Run the program again. Now, instead of seeing the object properties, we just get a generic message, “Instance of Profile”.

So as you can see, toString is pretty useful. And remember, you don’t have to print out just the properties, but any string representation of your object. That is, whatever works best in the way you are using it.