← → or space · progress saves for Continue on the roadmap
Goal
Know when to inherit implementation versus redeclare an entire interface.
Step 1 - extends (single superclass)
class Animal {
void breathe() => print('breathing');
}
class Dog extends Animal {
void bark() => print('woof');
}
void main() {
Dog d = Dog();
d.breathe();
}- You get superclass methods and fields. Override with
@overridewhen you replace behavior.
Step 2 - implements (no inherited body)
class Console {
void write(String s) => print(s);
}
class FileSink implements Console {
@override
void write(String s) {
print('[file] $s');
}
}
void main() {
Console c = FileSink();
c.write('hi');
}FileSinkmust provide every member ofConsoleeven ifConsolehad defaults (those defaults are not copied viaimplements).
Step 3 - Rules of thumb
extends: true subtype, reuse and specialize behavior.implements: adapt an existing class shape, or satisfy multiple interfaces.- Dart allows only one
extends; you canimplementsmany types.
Practice tasks
- Make an
abstract class Storagewithvoid save(String key, String value)andString? load(String key). Implement it twice:MemoryStorageandLoggingStoragethat delegates to anotherStorage. - Compare:
extendsa class with a non-empty method body vsimplementsthe same class; note what you must reimplement.