Recap - Animal System
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 56 of 110.
Challenge
EasyLet's build a complete animal system that brings together everything you've learned about mixins! You'll create a base Animal class, behavior mixins that use the on keyword, and specific animal classes that combine these capabilities in different ways.
You'll organize your code into three files:
animal.dart: Define your base class that all animals will inherit from:- An
Animalclass with aString nameproperty and a constructor - A method
describe()that prints[name] is an animal
- An
behaviors.dart: Import the animal file and create three behavior mixins, each restricted toAnimalusing theonkeyword:- A
Swimmingmixin with a methodswim()that prints[name] is swimming - A
Flyingmixin with a methodfly()that prints[name] is flying - A
Walkingmixin with a methodwalk()that prints[name] is walking
- A
main.dart: Import both files and create three animal classes that combine inheritance with different mixin combinations:- A
Duckclass that extendsAnimaland uses all three mixins (Swimming, Flying, Walking) - A
Fishclass that extendsAnimaland uses only theSwimmingmixin - A
Birdclass that extendsAnimaland usesFlyingandWalkingmixins
- Create a
Ducknamed'Donald', calldescribe(), thenswim(),fly(), andwalk() - Print an empty line
- Create a
Fishnamed'Nemo', calldescribe(), thenswim() - Print an empty line
- Create a
Birdnamed'Tweety', calldescribe(), thenfly()andwalk()
- A
Each mixin can access the name property because of the on Animal constraint, ensuring type safety while providing flexible behavior composition.
Expected output:
Donald is an animal
Donald is swimming
Donald is flying
Donald is walking
Nemo is an animal
Nemo is swimming
Tweety is an animal
Tweety is flying
Tweety is walkingTry it yourself
import 'animal.dart';
import 'behaviors.dart';
// TODO: Create Duck class that extends Animal and uses Swimming, Flying, Walking mixins
class Duck {
// TODO: Implement Duck class
}
// TODO: Create Fish class that extends Animal and uses only Swimming mixin
class Fish {
// TODO: Implement Fish class
}
// TODO: Create Bird class that extends Animal and uses Flying and Walking mixins
class Bird {
// TODO: Implement Bird class
}
void main() {
// TODO: Create a Duck named 'Donald'
// Call describe(), swim(), fly(), walk()
// TODO: Print an empty line
// TODO: Create a Fish named 'Nemo'
// Call describe(), swim()
// TODO: Print an empty line
// TODO: Create a Bird named 'Tweety'
// Call describe(), fly(), walk()
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor