Extending Enums in Java: The Interface Pattern vs Sealed Interfaces | Effective Java L39
The Modern SDE
0:00 / 0:00
Extending Enums in Java: The Interface Pattern vs Sealed Interfaces | Effective Java L39
5 просмотров · 12 ч назад
The Modern SDE
1,26 тыс. подписчиков
5 просмотров · 12 ч назад
Lecture 39 of the Effective Java Masterclass by Engineer X · Part 5: Enums and Annotations.
In this lecture
• A merchant tries to extend a library enum: enum classes are not extensible
• Why enums are closed: extension semantics, listing constants, and everything built on a fixed set
• The pattern: an interface for the type, a standard enum implementing it, APIs that take the interface
• Client enums implement the same interface; the abstract method lives in the interface, shared code in default methods
• Passing a whole enum type: Class‹T extends Enum‹T› & I› (EnumSet and EnumMap possible) vs Collection‹? extends I› (mixes types)
• Limits: no implementation inheritance between enums; the JDK's LinkOption implements CopyOption and OpenOption
• Modern Java: sealed interfaces over several enums, exhaustive switches with qualified constants (Java 21), closed vs open
• Interview: letting library users add options, and how that differs from sealed
Chapters
0:00 The enum you can't extend
0:35 Why enums are closed
1:12 Interface plus enum
2:04 Passing a whole enum type
3:33 The limits
4:16 Modern Java: sealed is the opposite
5:37 Interview: letting users add options
6:15 Recap and quiz
Follows the topics of Effective Java, 3rd edition, by Joshua Bloch (Item 38: Emulate extensible enums with interfaces). Every explanation, example and line of code in this lecture is original.
Previous lecture: L038 Java EnumMap vs Ordinal-Indexed Arrays: Group by Enum the Safe Way
Next lecture: L040 Java Annotations and Reflection: Build a Command Router, Not a Naming Pattern
#Java #EffectiveJava #JavaInterview #SeniorJavaDeveloper #JavaBestPractices #EngineerX #JavaEnums #SealedInterfaces #JavaInterfaces #EffectiveJava #JavaInterview