Think of traits like skill badges. A Scout can earn "Swimming", "Cooking", and "First Aid" badges — mixing multiple skills into one person. That's traits.
| Feature | Scala Trait | Java Interface |
|---|---|---|
| Concrete methods | ✅ Yes | ✅ Since Java 8 (default) |
| State (fields) | ✅ Can have val/var | ❌ No instance state |
| Multiple inheritance | ✅ Mix many traits | ✅ Implement many |
| Constructor params | ✅ Scala 3 traits | ❌ No |
| Linearization | ✅ Clear resolution order | ❌ Diamond problem |
Think of implicits like auto-fill on a form. The browser fills in your name, email, and address without you typing — because it "implicitly" knows the values from context.
implicit keyword says: "this parameter is optional — look for a matching value nearby."ExecutionContext for Futures, JSON formatters, database connections, configuration objects
Too many implicits make code hard to follow. "Where did that value come from?!" Use sparingly and clearly.
Imagine a universal travel adapter that automatically converts your plug to fit any outlet. That's an implicit conversion — automatic type transformation.
Think of a parking garage. A "Vehicles" garage (+covariant) accepts Cars. A "compact car wash" (-contravariant) can wash any Vehicle. A "Tesla-only charger" (=invariant) only works with Teslas.
"If Dog is a subtype of Animal, then List[Dog] IS a subtype of List[Animal]." Used for producers (things that give you values).
"If Dog is a subtype of Animal, then Trainer[Animal] can be used as Trainer[Dog]." Used for consumers (things that take values).
"Array[Dog] and Array[Animal] are completely unrelated." Used when the type both reads and writes. Strictest and safest.
greet (which needs an implicit String). What happens?