Records and Generics
27 interview questions and answers in the Records and Generics section.
Questions in this section
- What is Record in Java and since which version are they available
- What are the main differences between Record and regular class
- Can you inherit from Record or have Record inherit from another class
- Can you add additional methods to a Record
- What methods are automatically generated for a Record
- Can you override constructor in Record
- What is compact constructor in Record
- Can you declare static fields and methods in Record
- Are Record fields final
- Can you use Record as a key in HashMap
- What are Generics in Java
- What are the advantages of using Generics
- What is type erasure
- Can you create an array of generic type
- What are bounded type parameters
- What is the difference between <? extends T> and <? super T>?
- What is PECS (Producer Extends Consumer Super)
- Can you use primitive types as generic parameters
- What are raw types and why should you avoid them
- What happens when trying to create an instance of generic type via new T()
- [What is the difference between List<?> and List
- Can you overload methods that differ only in generic parameters?
- What is recursive type bound
- How do Generics work with inheritance
- What are bridge methods and why are they needed
- Can you use multiple bounds for a single type parameter?
- How to implement Singleton pattern with Record
Study navigator
27 questions for Middle Java Developer interview preparation.
π All Questions
πΊοΈ Topic Dependency Map
ββββββββββββββββββββββββββββββββββββββββββββ
β RECORDS (1-10) β
β 1. What is Record (Java 16) β
β 2. Record vs regular class β
β 3. Inheritance (not allowed) β
β 4. Additional methods β
β 5. Auto-generated methods β
β 6. Constructors β
β 7. Compact constructor β
β 8. Static fields and methods β
β 9. Final fields β
β 10. Record as HashMap key β
ββββββββββββββββββββ¬ββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β GENERICS β β WILDCARDS β β ADVANCED β
β BASICS β β AND ERASURE β β GENERICS β
β (11-12, 18) β β (13-14, 19-21) β β (22-26) β
β 11. Generics β β 13. Erasure β β 22. Overloading β
β 12. Advantages β β 14. Generic β β 23. Recursive boundβ
β 18. Primitives β β arrays β β 24. Inheritance β
β β β 19. Raw types β β 25. Bridge methods β
β β β 20. new T() β β 26. Multiple boundsβ
β β β 21. List<?> vs β β β
β β β List<Obj> β β β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ ββββββββββ¬ββββββββββββ
β β β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β PATTERNS (15-17, 27) β
β 15. Bounded type parameters β
β 16. extends T vs super T β
β 17. PECS β
β 27. Singleton with Record β
ββββββββββββββββββββββββββββββββββββββββββββ
π― Recommended Study Order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Records basics | Q1-Q3 | What is Record, differences from class, cannot inherit |
| 2 | Record methods | Q4, Q5, Q8, Q9 | Additional methods, auto-generation, static, final |
| 3 | Generics basics | Q11, Q12 | What are generics, why they are needed |
| 4 | Primitives | Q18 | Autoboxing overhead, why primitives are not allowed |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Record constructors | Q6, Q7 | Overriding, compact constructor |
| 2 | Record + HashMap | Q10 | Why Records are ideal as keys |
| 3 | Type Erasure | Q13, Q14 | How erasure works, generic arrays |
| 4 | Bounded types | Q15, Q19 | Type constraints, raw types |
| 5 | Wildcards | Q21, Q20 | List<?> vs List |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | PECS | Q16, Q17 | Producer Extends, Consumer Super |
| 2 | Overloading | Q22 | Cannot overload by generics |
| 3 | Recursive bounds | Q23 | Comparable<? super T>, CRTP |
| 4 | Inheritance + Generics | Q24 | Covariant returns, generics invariance |
| 5 | Bridge methods | Q25 | Why needed, how they are generated |
| 6 | Multiple bounds | Q26 | Intersection types, first bound erasure |
π Key Topic Connections
Topic: Records
Q1 (Record) β Q2 (vs regular class) β Q3 (inheritance)
β
Q4 (Additional methods) β Q5 (Auto-generation) β Q6 (Constructors) β Q7 (Compact)
β
Q8 (Static) β Q9 (Final) β Q10 (HashMap key)
Topic: Generics Basics
Q11 (Generics) β Q12 (Advantages) β Q18 (Primitives)
Topic: Erasure and Wildcards
Q13 (Type Erasure) β Q14 (Generic arrays) β Q19 (Raw types) β Q20 (new T()) β Q21 (List<?> vs List<Object>)
Topic: PECS
Q15 (Bounded types) β Q16 (extends vs super) β Q17 (PECS)
Topic: Advanced
Q22 (Overloading) β Q23 (Recursive bound) β Q24 (Inheritance) β Q25 (Bridge methods) β Q26 (Multiple bounds)
π Cheat Sheet: What to Know for Each Level
π’ Junior
- Record = immutable data class, Java 16+
- Auto-generated: constructor, accessors, equals/hashCode/toString
- Cannot inherit, cannot have instance fields (only static)
- Generics = type parameterization, compile-time type safety
- Primitives cannot be used in generics β autoboxing to wrappers
π‘ Middle
- Compact constructor: no signature, validation only
- Type erasure: generics exist only at compile-time, runtime = Object
- Raw types β backwards compatibility, but unchecked warnings
<? extends T>= read-only (producer),<? super T>= write (consumer)new T()is impossible β type variable has no constructor
π΄ Senior
- PECS: Producer Extends, Consumer Super
- Bridge methods: compiler generates for covariant returns after erasure
- Multiple bounds:
<T extends A & B>β erasure by first bound - Generic arrays are impossible β heap pollution risk
- List
is NOT a subtype of List β generics are invariant - Recursive bound:
T extends Comparable<? super T>for inheritable compareTo
π Format of Each File
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, common mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview Cheat Sheet β key points, common questions, red flags, related topics