Java Generics Interview Questions and Answers
-
What are Java Generics?
- Answer: Java Generics allow you to write type-safe code by parameterizing classes, interfaces, and methods with type parameters. This allows you to reuse code with different data types without sacrificing type safety. Before generics, this was often achieved with Object types, leading to frequent casting and potential runtime exceptions (ClassCastException).
-
What is the benefit of using Generics?
- Answer: Generics improve type safety by catching type errors at compile time instead of runtime. They reduce code duplication by allowing you to write one generic method or class that can work with various data types. They improve code readability and maintainability by making the intent clearer.
-
Explain the concept of type parameters in Generics.
- Answer: Type parameters are placeholders for concrete types that are specified when a generic class or method is used. They are typically denoted by uppercase letters (e.g., T, K, V) within angle brackets (<>). For example, in `List
`, `String` is the concrete type replacing the type parameter `T` in the generic `List `.
- Answer: Type parameters are placeholders for concrete types that are specified when a generic class or method is used. They are typically denoted by uppercase letters (e.g., T, K, V) within angle brackets (<>). For example, in `List
-
How do you declare a generic class?
- Answer: A generic class is declared by placing type parameters within angle brackets after the class name. For example: `public class MyGenericClass
{ ... }` The `T` is a type parameter, which can be replaced with any concrete type when an object of `MyGenericClass` is created (e.g., `MyGenericClass `, `MyGenericClass `).
- Answer: A generic class is declared by placing type parameters within angle brackets after the class name. For example: `public class MyGenericClass
-
How do you declare a generic method?
- Answer: A generic method is declared by placing type parameters within angle brackets before the return type of the method. For example: `public
T myGenericMethod(T input) { ... }` The `T` is a type parameter, and the method can operate on any type passed as the argument.
- Answer: A generic method is declared by placing type parameters within angle brackets before the return type of the method. For example: `public
-
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 replaced with their upper bounds (usually `Object`). This is why you can't get the type parameter at runtime using `instanceof` with generics.
-
What are unbounded wildcards?
- Answer: Unbounded wildcards are represented by `?`. They specify that a method or class can accept any type. For example, `List>` can hold a list of any type.
-
What are upper bounded wildcards?
- Answer: Upper bounded wildcards are represented by ` extends T>`, where `T` is an upper bound. They specify that a method or class can accept any subtype of `T`. For example, `List extends Number>` can hold a list of Integers, Doubles, or any other Number subtype.
-
What are lower bounded wildcards?
- Answer: Lower bounded wildcards are represented by ` super T>`, where `T` is a lower bound. They specify that a method or class can accept any supertype of `T`. For example, `List super Integer>` can hold a list of Integers, Numbers, or any other supertype of Integer.
-
Explain the PECS principle.
- Answer: PECS stands for "Producer Extends, Consumer Super." It's a guideline for choosing wildcard types: use ` extends T>` when you only need to *produce* (read) elements from a collection, and ` super T>` when you only need to *consume* (write) elements to a collection.
-
Can you create an array of generic type?
- Answer: No, you cannot directly create an array of a generic type. This is due to type erasure. However, you can create arrays of Object and then cast them, but this loses the type safety benefits of generics.
-
What is a generic interface? Give an example.
- Answer: A generic interface is an interface that uses type parameters. For example: `interface MyGenericInterface
{ T getValue(); void setValue(T value); }` This interface can be implemented by classes specifying the concrete type for `T` (e.g., `class MyClass implements MyGenericInterface { ... }`)
- Answer: A generic interface is an interface that uses type parameters. For example: `interface MyGenericInterface
-
Explain how generics handle checked exceptions.
- Answer: Generics do not directly handle checked exceptions. If a method within a generic class or method throws a checked exception, that exception still needs to be handled (or declared) in the same way as in non-generic code.
-
Can you have multiple type parameters in a generic class?
- Answer: Yes, you can have multiple type parameters in a generic class. For example: `class Pair
{ K key; V value; ... }` This declares a class with two type parameters, `K` (key) and `V` (value).
- Answer: Yes, you can have multiple type parameters in a generic class. For example: `class Pair
-
What is the difference between `List
` and `List extends Number>`? - Answer: `List
` is a list that can only hold Integers. `List extends Number>` is a list that can hold any subtype of Number (Integer, Double, Float, etc.). You can read from a `List extends Number>`, but you can't add anything other than null.
- Answer: `List
Thank you for reading our blog post on 'Java Generics Interview Questions and Answers'.We hope you found it informative and useful.Stay tuned for more insightful content!