Object-Oriented Programming in Dart
Object-Oriented Programming teaches you to design your own types.
In this chapter, you will:
- write classes with fields and methods, and make objects from them
- build objects in different ways with constructors
- describe fixed sets of choices with enums
- hide data, reuse code with inheritance, and write abstract classes and interfaces
- use polymorphism, mixins, class modifiers, generics, composition and extensions
What each lesson covers:
- Classes: How to design classes with fields and methods, make objects, use this, static members and cascades, and build objects with default, named, redirecting, const and factory constructors.
- Enums: How to make a fixed set of choices with enums, read their name and index, add fields and methods with enhanced enums, and use enums in switch.
- Encapsulation: How to hide an object's data with private names, and control how it is read and changed with getters, setters and computed properties.
- Inheritance: How to build a child class on a parent with extends, override and use super, and when to build objects from parts instead, with aggregation and composition.
- Polymorphism: How abstract classes and interfaces set out what a class must do, and how polymorphism lets one call run a different method for each kind of object.
- Mixins: How to add ready-made abilities to classes with mixins, make callable classes, and add methods to existing types like String and List with extensions and extension types.
- Class Modifiers: How abstract, base, interface, final, sealed and mixin class control who can make, extend or implement a class, and how sealed classes make switch check every case.
- Generics: How to write classes and functions with type parameters like T, so one piece of code works safely with many types.
This chapter has 8 lessons. Plan on about 169 minutes, with 36 exercises to check what you learned. Every example is real Dart code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.
Simple lesson: finish this chapter and you can model a whole school in code: students, teachers and houses.
Lessons in this section
- Classes
How to design classes with fields and methods, make objects, use this, static members and cascades, and build objects with default, named, redirecting, const and factory constructors.
- Write classes with fields, methods and static members, and make objects
- Use this and the cascade operator
- Write every kind of constructor: parameterized, named, initializer lists, redirecting, const and factory
- Enums
How to make a fixed set of choices with enums, read their name and index, add fields and methods with enhanced enums, and use enums in switch.
- Create enums and use values, name and index
- Write enhanced enums with fields and methods
- Use enums in comparisons and switch
- Encapsulation
How to hide an object's data with private names, and control how it is read and changed with getters, setters and computed properties.
- Hide fields with private _ names
- Write getters, read-only properties and computed properties
- Check values in setters before saving them
- Inheritance
How to build a child class on a parent with extends, override and use super, and when to build objects from parts instead, with aggregation and composition.
- Create child classes with extends, override methods and use super
- Model has-a relationships with aggregation and composition
- Choose between inheritance and composition
- Polymorphism
How abstract classes and interfaces set out what a class must do, and how polymorphism lets one call run a different method for each kind of object.
- Write abstract classes and implement interfaces
- Choose between extends and implements
- Use parent-type variables and runtime method resolution
- Mixins
How to add ready-made abilities to classes with mixins, make callable classes, and add methods to existing types like String and List with extensions and extension types.
- Create mixins, use several with with, and limit them with on
- Make callable classes with call()
- Write extension methods and getters, and explain extension types
- Class Modifiers
How abstract, base, interface, final, sealed and mixin class control who can make, extend or implement a class, and how sealed classes make switch check every case.
- Read what each class modifier allows outside its library
- Choose a modifier for a class you write
- Use sealed classes with an exhaustive switch
- Generics
How to write classes and functions with type parameters like T, so one piece of code works safely with many types.
- Explain why generics beat dynamic for reuse and safety
- Write generic classes and generic functions
- Limit type parameters with extends