Leave a rating/review
Often times when you write a subclass, you want to make a change in the way an object behaves. This can result in adding new methods but also, you change existing methods as well. This is known as overriding. When we override a method, you can provide a different implementation of that method. But yet, there are times where we still want to use the original method.
We can still do this. We can simply call it using the super keyword. The super keyword represents the immediate parent class. You simply call methods on super like you would a normal variable. In fact, you can use super to even call the parent’s constructors as you’ll see soon enough.
Now when you do override a method, you need to mark that method as overridden. You do this by placing the annotation, @override in front of it. In dart, this is known as metadata. This provides information about your code such as marking a method to be overridden or also marking a method deprecated in that you plan to no longer use it.
You can define your metadata annotations, but that’s beyond this course. The thing to keep in mind is that the override lets both you and dart know that you overriding a parent class’ method.
In this demo, we’re going to outline a teacher class and a student teacher. The teacher class will take in a list of grades and calling the method, getAverage will return the average grade. The StudentTeacher object will also get the average, but for the purposes of this demo, it will add an additional five points.
Open up Dartpad.dev in your browsers. We’ll get started by defining our teacher class.
class Teacher {
List<int> grades;
Teacher(this.grades);
int getAverage() {
int sum = 0;
for (var grade in grades) {
sum += grade;
}
return sum ~/ grades.length;
}
}
This is a standard class that takes in a list of ints after which, it will return the average. You’ll notice in the return statement there is a sum - tilda forward slash. This is a truncating division operator, meaning it will round the number down to an integer versus a floating point number.
Now lets define create a StudentTeacher subclass.
class StudentTeacher extends Teacher {
}
Now lets create a constructor. Add the following constructor.
StudentTeacher(this.grades);
This should work. We just used a constructor similar to this. But we’re trying to set our grades to two properties that don’t exist in our class. They exist in the parent class. We can still access them, but we need to set them in the parent. This means we have to call our parent class constructor.
Change it to the following:
StudentTeacher(super.grades);
First we define the grades, then use the keyword super, passing in the grades. Remember, the super just means the parent class. Now the values are set so we can continue.
Let’s override the getAverage method. First, we need to mark it as overridden by using the override annotation.
@override
Now lets add the method.
int getAverage() => super.getAverage() + 5;
Here we call the parent method and then add five to it. We get the benefit of the method without have to duplicate code. Now lets define some grades in main.
var grades = [68, 80, 96, 37];
Then we will define a teacher and a student teacher class.
var teacher = Teacher(grades);
var studentTeacher = StudentTeacher(grades);
Finally, lets print out the grades.
print(teacher.getAverage());
print(studentTeacher.getAverage());
Run the program. And we get our results. The student teacher has averaged five points more. Nice work!