1 2 3 4 5 6 7 8 9 |
class Animal { // used named optional parameter so we know what the values mean in child classes Animal({required this.age}) { print('√ Constructing Animal object with $this.age'); } final int age; void sleep() => print('sleep'); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import 'Animal.dart'; class Dog extends Animal { String type = ""; String? name; // since Animal has named optional param age, we must specify age in super() // let's use named optional param to better describe what they mean Dog({required int age, required String type, String? name}) : super(age: age) { print('√ Constructing a $type Dog that is $age years old'); this.type = type; this.name = name ?? "no name given"; } void bark() => print('bark'); @override void sleep() { super.sleep(); print(this.age < 2 ? 'Sleep some more' : 'Go play outside'); } } |