Java Generics Interview Questions and Answers for experienced

100 Java Generics Interview Questions and Answers
  1. What are Java Generics?

    • Answer: Java Generics are a powerful feature introduced in Java 5 that allows you to write type-safe code. They enable you to parameterize types with type variables, allowing the compiler to enforce type constraints at compile time, preventing runtime ClassCastException errors. This improves code readability, maintainability, and reduces the risk of runtime type errors.
  2. Explain the benefits of using Generics.

    • Answer: Generics offer several key benefits: Type safety (eliminating ClassCastException), improved code readability (more self-documenting code), reduced code duplication (writing generic methods/classes applicable to various types), and better performance in some cases (due to type erasure, avoiding runtime type checks).
  3. What is type erasure?

    • Answer: Type erasure is the process by which the Java compiler removes generic type information during compilation. At runtime, the generic type parameters are not available. This means that `List` and `List` are both treated as `List` at runtime.
  4. How does type erasure impact the use of Generics?

    • Answer: Type erasure means you cannot create arrays of generic types (e.g., `new List[10]`), you cannot use instanceof with generic types (e.g., `object instanceof List` will not compile), and you cannot get the generic type parameter at runtime using reflection alone.
  5. Explain wildcard types in Generics.

    • Answer: Wildcard types allow you to write more flexible generic code. `?` represents an unknown type. `? extends Number` means the type can be Number or any subtype, while `? super Integer` means the type can be Integer or any supertype.
  6. What is the difference between `? extends T` and `? super T`?

    • Answer: `? extends T` represents an upper bound; the wildcard can be T or any subtype. You can read values from it but not write. `? super T` represents a lower bound; the wildcard can be T or any supertype. You can write values of type T into it, but not read values out unless you know the exact type.
  7. Explain unbounded wildcard types.

    • Answer: An unbounded wildcard (`?`) means that the type can be any type. It's the least restrictive wildcard type. You can only use methods defined in Object for unbounded wildcards.
  8. How do you create a generic class?

    • Answer: You declare a generic class using angle brackets `` after the class name, where `T` is a type parameter (you can use other names like `E`, `K`, `V`). For example: `public class MyGenericClass { ... }`
  9. How do you create a generic method?

    • Answer: You declare a generic method by adding angle brackets `` before the return type. For example: `public T myGenericMethod(T input) { ... }`
  10. What is a bounded wildcard? Provide an example.

    • Answer: A bounded wildcard restricts the type of the wildcard. For example, `List` only accepts Lists of Number or its subtypes (like Integer, Double). This allows you to read Number objects but not write arbitrary objects.
  11. Can you have multiple type parameters in a generic class or method? Give an example.

    • Answer: Yes. For example: `public class Pair { private K key; private V value; ... }` This defines a class with two type parameters, `K` for key and `V` for value.
  12. Explain the concept of generic interfaces.

    • Answer: Generic interfaces are interfaces that use type parameters. They define a contract that classes can implement using specific types. For example, `interface MyGenericInterface { T process(T input); }`
  13. How do you use generics with collections?

    • Answer: Collections like ArrayList, HashMap, etc., are typically used with generic types to specify the type of elements they hold. For example, `List stringList = new ArrayList<>();`
  14. What are some common pitfalls to avoid when using Generics?

    • Answer: Common pitfalls include misunderstanding type erasure, incorrect use of wildcards (leading to compile-time errors or unexpected runtime behavior), and attempting to create arrays of generic types.
  15. Explain the use of Generics in exception handling.

    • Answer: Generics can improve exception handling by allowing you to create custom exceptions that are type-safe. For example, a `MyGenericException` could carry a T object representing the specific error context.
  16. Describe how generics enhance code reusability.

    • Answer: Generics enhance reusability by creating type-safe code that works with different types without needing to rewrite code for each type. A single generic method or class can operate on various types.
  17. How can you achieve runtime type information with generics? (Despite type erasure)

    • Answer: While type erasure removes generic type information at runtime, you can use techniques like reflection to inspect the runtime type of objects. However, you'll be working with the raw types, not the generic types. You can also add type information yourself (e.g., store the type in a field). Finally, using instanceof (against the raw type) can help you know the type at runtime, however, you loose all benefits of generics.
  18. Explain the difference between raw types and parameterized types.

    • Answer: Parameterized types (e.g., `List`) use generics to specify the type. Raw types (e.g., `List`) are essentially the pre-generics way of using collections; they lose type safety. Using raw types is generally discouraged.

Thank you for reading our blog post on 'Java Generics Interview Questions and Answers for experienced'.We hope you found it informative and useful.Stay tuned for more insightful content!