Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 2: Learn Inheritance

13. Override Methods

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: 12. Understand Inheritance Next episode: 14. Challenge: Override a Method

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.

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.

class Teacher {
  
  List<int> grades;
  
  Teacher(this.grades);
  
  int getAverage() {
    int sum = 0;
    for (var grade in grades) {
      sum += grade;
    }
    return sum ~/ grades.length;
  }
}
class StudentTeacher extends Teacher {

}
StudentTeacher(this.grades);
StudentTeacher(super.grades);
@override
int getAverage() => super.getAverage() + 5;
var grades = [68, 80, 96, 37];
var teacher = Teacher(grades);
var studentTeacher = StudentTeacher(grades);
print(teacher.getAverage());
print(studentTeacher.getAverage());