Classes and Objects
- Classes. A class takes constructor parameters in its header: fields, constructor and body in one place.
- object. An object is a built-in singleton: companion objects hold factories and shared definitions.
- apply. The apply method makes objects callable: User("Ada") is User.apply("Ada"), by convention.
Traits
- Traits. A trait declares interface and mixable implementation: Scala’s unit of behaviour composition.
- Mixin composition. Classes extend one class but mix many traits: behaviour stacked at declaration.
- Linearisation. Mixin order resolves by linearisation: a defined, inspectable answer to “whose method wins”.
Composition
- Composition first. Scala favours composition: small traits and case classes wired together over deep towers.
- Self types. A self type declares what a trait requires of its host: dependencies stated, not smuggled.
- Sealed boundaries. sealed closes a hierarchy to its file: the compiler then knows every subtype that exists.
Before and after this level
- Before: Level 2: Collections: Immutable Collections, Transformation Operations, Lazy and Eager Processing.
- Next: Level 4: Algebraic Data Types: Enums and Case Classes, Pattern Matching, Modelling Valid States.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Scala Level 3: Classes & Traits cover?
Classes and Objects, Traits, Composition: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 2: Collections, which covers Immutable Collections, Transformation Operations, Lazy and Eager Processing.