On this page12 sections
Lecture 043: Overriding & Overloading
| Day | Extra Topics |
| Lecture | 043 of 057 |
| Estimated video length | 10 min |
| Target | Dart 3.12 |
| Level | Beginner |
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:
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:
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();
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:
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:
int add(int a, int b) => a + b;
int add(int a, int b, int c) => a + b + c;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:
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.0Code Examples
Example 1: Shapes That Describe Themselves
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
Shape with area 0.0
Circle with area 12.6
Rectangle with area 12.0toString() 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
class Dog extends Animal {
@override
void sound(String loud) => print('WOOF');
}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:
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
- What does
@overridetell Dart? - How do you run the parent's version of a method from inside the override?
- What does
printshow for an object whose class doesn't overridetoString()? - Does Dart support method overloading?
- Rewrite two "overloads"
greet()andgreet(String name)as one Dart function.
Practice Answers
- That this method replaces one inherited from the parent. Dart checks that the parent really has it.
- Call
super.methodName(). Instance of 'ClassName'.- No. Each name can be declared only once.
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.