Menu
Coddy logo textTech

The @override Annotation

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 39 of 110.

The @override annotation is a best practice marker that tells Dart (and other developers) that you're intentionally replacing a method from a parent class. While method overriding works without it, using @override adds an important safety net.

class Animal {
  String name;
  
  Animal(this.name);
  
  void speak() {
    print('$name makes a sound');
  }
}

class Dog extends Animal {
  Dog(String name) : super(name);
  
  @override
  void speak() {
    print('$name barks: Woof!');
  }
}

The annotation provides two key benefits. First, it documents your intent clearly - anyone reading the code immediately knows this method exists in the parent class. Second, Dart's analyzer will warn you if the parent method doesn't actually exist, catching typos and refactoring mistakes early.

Consider what happens if you misspell the method name without @override:

class Cat extends Animal {
  Cat(String name) : super(name);
  
  void speek() {  // Typo! No warning without @override
    print('$name meows');
  }
}

Without the annotation, Dart assumes you're creating a new method called speek(). With @override, the analyzer would immediately flag that no such method exists in the parent class. This simple annotation prevents subtle bugs that can be difficult to track down later.

challenge icon

Challenge

Easy

Let's build a media player system that demonstrates the importance of the @override annotation when customizing inherited behavior. You'll create different media types that each play content in their own way.

You'll organize your code into two files:

  • media.dart: Define your media hierarchy here:
    • A Media class with a String title property, a constructor that takes the title, and a method play() that prints Playing: [title]
    • A Song class that extends Media and adds a String artist property. Use the @override annotation and override play() to print Playing song: [title] by [artist]
    • A Video class that extends Media and adds an int duration property (in seconds). Use @override and override play() to first call the parent's version with super.play(), then print Duration: [duration] seconds
  • main.dart: Import your media file and demonstrate the overridden methods:
    • Create a Media object with title 'Generic Content' and call play()
    • Print an empty line
    • Create a Song with title 'Imagine' and artist 'John Lennon', then call play()
    • Print an empty line
    • Create a Video with title 'Dart Tutorial' and duration 360, then call play()

The @override annotation documents your intent and helps catch errors. If you accidentally misspelled play() as plya(), the annotation would alert you that no such method exists in the parent class.

Expected output:

Playing: Generic Content

Playing song: Imagine by John Lennon

Playing: Dart Tutorial
Duration: 360 seconds

Cheat sheet

The @override annotation marks methods that intentionally replace a parent class method. While optional, it provides safety and clarity.

class Animal {
  String name;
  
  Animal(this.name);
  
  void speak() {
    print('$name makes a sound');
  }
}

class Dog extends Animal {
  Dog(String name) : super(name);
  
  @override
  void speak() {
    print('$name barks: Woof!');
  }
}

Benefits of @override:

  • Documents intent - shows the method exists in the parent class
  • Catches errors - Dart's analyzer warns if the parent method doesn't exist, preventing typos

Without @override, typos create new methods instead of flagging errors:

class Cat extends Animal {
  Cat(String name) : super(name);
  
  void speek() {  // Typo creates new method without warning
    print('$name meows');
  }
}

You can call the parent's version using super:

@override
void play() {
  super.play();  // Call parent method first
  print('Additional behavior');
}

Try it yourself

import 'media.dart';

void main() {
  // TODO: Create a Media object with title 'Generic Content' and call play()
  
  
  // TODO: Print an empty line
  
  
  // TODO: Create a Song with title 'Imagine' and artist 'John Lennon', then call play()
  
  
  // TODO: Print an empty line
  
  
  // TODO: Create a Video with title 'Dart Tutorial' and duration 360, then call play()
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming