On this page12 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. Overriding a Method
  4. 2. Calling the Parent's Version With super
  5. 3. Overriding toString()
  6. 4. Overloading: Dart Doesn't Have It
  7. 5. The Dart Way: Optional and Named Parameters
  8. Code Examples
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 043: Overriding & Overloading

DayExtra Topics
Lecture043 of 057
Estimated video length10 min
TargetDart 3.12
LevelBeginner

A child class often needs to do the same job differently: every animal makes a sound, but a dog says "Woof". Overriding replaces a parent's method in the child. Overloading (one name, different parameters) works differently in Dart, and this lecture shows how.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lecture 042 (inheritance) and lectures 026–027 (function parameters).


Learning Objectives

  • Override a parent method with @override
  • Call the parent's version with super
  • Override toString() to print objects nicely
  • Understand that Dart has no method overloading
  • Get the same effect with optional and named parameters

1. Overriding a Method

Overriding means a child class writes its own version of a method it inherited. Mark it with @override:

dart
class Animal {
  void sound() {
    print('Some generic sound');
  }
}

class Dog extends Animal {
  @override
  void sound() {
    print('Woof!');
  }
}

void main() {
  Animal().sound();   // Some generic sound
  Dog().sound();      // Woof!
}

The new method must keep the same name and the same parameters as the original.

2. Calling the Parent's Version With super

Sometimes you want to add to the parent's behaviour instead of replacing it:

dart
class Animal {
  void describe() {
    print('I am an animal.');
  }
}

class Dog extends Animal {
  @override
  void describe() {
    super.describe();               // run Animal's version first
    print('More exactly, I am a dog.');
  }
}

Output of Dog().describe();

code
I am an animal.
More exactly, I am a dog.

3. Overriding toString()

Every class quietly inherits toString(), which print uses. Override it to control how your object prints:

dart
class Point {
  int x, y;
  Point(this.x, this.y);

  @override
  String toString() => 'Point($x, $y)';
}

print(Point(2, 3));   // Point(2, 3)

Without the override, print shows Instance of 'Point'.

4. Overloading: Dart Doesn't Have It

In some languages you can write several functions with the same name but different parameters. That's overloading. Dart doesn't allow it:

dart
int add(int a, int b) => a + b;
int add(int a, int b, int c) => a + b + c;
code
Error: 'add' is already declared in this scope.

5. The Dart Way: Optional and Named Parameters

One function with optional parameters does the job of several overloads:

dart
int add(int a, int b, [int c = 0]) => a + b + c;

double area({double? radius, double? width, double? height}) {
  if (radius != null) return 3.14 * radius * radius;
  return (width ?? 0) * (height ?? 0);
}

print(add(1, 2));                  // 3
print(add(1, 2, 3));               // 6
print(area(radius: 1));            // 3.14
print(area(width: 2, height: 5));  // 10.0

Code Examples

Example 1: Shapes That Describe Themselves

dart
class Shape {
  String get name => 'Shape';
  double area() => 0;

  @override
  String toString() => '$name with area ${area().toStringAsFixed(1)}';
}

class Circle extends Shape {
  final double radius;
  Circle(this.radius);

  @override
  String get name => 'Circle';

  @override
  double area() => 3.14159 * radius * radius;
}

class Rectangle extends Shape {
  final double width, height;
  Rectangle(this.width, this.height);

  @override
  String get name => 'Rectangle';

  @override
  double area() => width * height;
}

void main() {
  print(Shape());
  print(Circle(2));
  print(Rectangle(3, 4));
}

Output

code
Shape with area 0.0
Circle with area 12.6
Rectangle with area 12.0

toString() is written once in Shape, but it calls name and area(), which each child overrides. So every shape prints its own details. Getters can be overridden too.


Common Beginner Mistakes

1. Changing the parameters when overriding

dart
class Dog extends Animal {
  @override
  void sound(String loud) => print('WOOF');
}
code
Error: The method 'Dog.sound' has more required arguments than those of overridden method 'Animal.sound'.

An override must accept the same arguments as the original.

2. @override on a method that doesn't exist in the parent: Dart warns:

code
warning - The method doesn't override an inherited method. Try updating this class to match
the superclass, or removing the override annotation.

Usually a typo in the name (for example sounds instead of sound).

3. Leaving out @override: the code still runs, but you lose that safety check. Always write it.

4. Trying to overload: two functions with the same name won't compile. Use optional or named parameters.


Practice Questions

  1. What does @override tell Dart?
  2. How do you run the parent's version of a method from inside the override?
  3. What does print show for an object whose class doesn't override toString()?
  4. Does Dart support method overloading?
  5. Rewrite two "overloads" greet() and greet(String name) as one Dart function.

Practice Answers

  1. That this method replaces one inherited from the parent. Dart checks that the parent really has it.
  2. Call super.methodName().
  3. Instance of 'ClassName'.
  4. No. Each name can be declared only once.
  5. void greet([String name = 'friend']) => print('Hello, $name!');

Key Takeaways

  • Overriding = a child class replaces an inherited method; mark it @override.
  • Keep the same name and parameters; super.method() reuses the parent's version.
  • Override toString() to make objects print nicely.
  • Dart has no overloading; use optional [ ] or named { } parameters instead.