extends and Method Overriding
Share and specialize parent behavior.
extends and Method Overriding is a free Dart Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Sharing Behavior
When two classes share most of their behavior, you can write it once and reuse it. Dart calls this inheritance, and it saves you from copy-paste. 🌱
The extends Keyword
You inherit by writing extends after a class name. The new class is a child that automatically gains the parent's fields and methods.
class Animal {}
class Dog extends Animal {}Parent and Child
The class you extend is the superclass (parent). The new one is the subclass (child). A Dog is an Animal, but not every Animal is a Dog.
Inherited Members
A child instantly gets to use the parent's public methods as if they were its own. You write inherited behavior once and every subclass benefits.
class Animal {
void breathe() => print('breathing');
}
class Dog extends Animal {}
// Dog().breathe() worksAdding New Behavior
A subclass can also add its own methods on top of what it inherits. The child stays an Animal but gains extra abilities the parent never had.
class Dog extends Animal {
void bark() => print('Woof!');
}Overriding a Method
Sometimes the parent's version is wrong for the child. Overriding lets a subclass replace an inherited method with its own implementation.
class Animal {
void speak() => print('...');
}
class Cat extends Animal {
void speak() => print('Meow');
}The @override Marker
Add the @override annotation above a replaced method. It tells the analyzer to confirm the parent really has that method, catching typos early.
class Cat extends Animal {
@override
void speak() => print('Meow');
}Same Signature Required
An override must keep the same name and a compatible signature. Change the parameters or return type carelessly and Dart will reject it.
Reusing the Parent Version
Inside an override you can still call the original through super, then add your own steps around it. You extend behavior instead of throwing it away.
@override
void speak() {
super.speak();
print('and purrs');
}Polymorphism in Action
Store children in an Animal variable and call speak: each runs its own version. This is polymorphism, the real payoff of overriding.
Animal a = Cat();
a.speak(); // MeowSingle Inheritance
A Dart class can extend exactly one superclass, never two. For sharing across unrelated trees you will later reach for mixins instead.
Quick Check
You replace an inherited method in a subclass. What does it do?
Recap
You used extends to inherit a parent's members, added new methods, and overrode others with @override. That is the heart of class reuse. 🎉
Frequently asked questions
Is the “extends and Method Overriding” lesson free?
Yes — the full text of “extends and Method Overriding” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “extends and Method Overriding”?
Share and specialize parent behavior. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “extends and Method Overriding” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- extends and Method Overriding
- Calling super and Constructor Chaining
- Mixins With the with Keyword
- on Constraints and Mixin Order