Section 20 Β· 27 questions

Records and Generics

27 interview questions and answers in the Records and Generics section.

English Records and Generics Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. What is Record in Java and since which version are they available
  2. What are the main differences between Record and regular class
  3. Can you inherit from Record or have Record inherit from another class
  4. Can you add additional methods to a Record
  5. What methods are automatically generated for a Record
  6. Can you override constructor in Record
  7. What is compact constructor in Record
  8. Can you declare static fields and methods in Record
  9. Are Record fields final
  10. Can you use Record as a key in HashMap
  11. What are Generics in Java
  12. What are the advantages of using Generics
  13. What is type erasure
  14. Can you create an array of generic type
  15. What are bounded type parameters
  16. What is the difference between <? extends T> and <? super T>?
  17. What is PECS (Producer Extends Consumer Super)
  18. Can you use primitive types as generic parameters
  19. What are raw types and why should you avoid them
  20. What happens when trying to create an instance of generic type via new T()
  21. [What is the difference between List<?> and List?](./21-what-is-the-difference-between-list-and-list-object/)
  22. Can you overload methods that differ only in generic parameters?
  23. What is recursive type bound
  24. How do Generics work with inheritance
  25. What are bridge methods and why are they needed
  26. Can you use multiple bounds for a single type parameter?
  27. How to implement Singleton pattern with Record

Study navigator

27 questions for Middle Java Developer interview preparation.


πŸ“‹ All Questions

# Question Difficulty
1 What is Record in Java and since which version are they available ⭐
2 What are the main differences between Record and regular class ⭐
3 Can you inherit from Record ⭐
4 Can you add additional methods to a Record ⭐⭐
5 What methods are automatically generated for a Record ⭐⭐
6 Can you override constructor in Record ⭐⭐
7 What is compact constructor in Record ⭐⭐
8 Can you declare static fields and methods in Record ⭐
9 Are Record fields final ⭐
10 Can you use Record as a key in HashMap ⭐⭐
11 What are Generics in Java ⭐
12 What are the advantages of using Generics ⭐
13 What is type erasure ⭐⭐⭐
14 Can you create an array of generic type ⭐⭐
15 What are bounded type parameters ⭐⭐
16 What is the difference between <? extends T> and <? super T> ⭐⭐⭐
17 What is PECS (Producer Extends Consumer Super) ⭐⭐⭐
18 Can you use primitive types as generic parameters ⭐
19 What are raw types and why should you avoid them ⭐⭐
20 What happens when you try to create an instance of generic type via new T() ⭐⭐
21 [What is the difference between List<?> and List](21.%20What%20is%20the%20difference%20between%20List%20and%20List%20Object.md) ⭐⭐
22 Can you overload methods that differ only in generic parameters ⭐⭐⭐
23 What is recursive type bound ⭐⭐⭐
24 How do generics work with inheritance ⭐⭐
25 What are bridge methods and why are they needed ⭐⭐⭐
26 Can you use multiple bounds for a single type parameter ⭐⭐⭐
27 How to implement Singleton pattern using Record ⭐⭐

πŸ—ΊοΈ 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              β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 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, new T()

πŸ”΄ 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