- Home
- Interview Questions
- Java
- What are the constraints in Java generics and why are they u…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: Constraints in Java generics limit the types that can be used as type arguments, ensuring type safety and flexibility.
Detailed explanation
In Java generics, constraints help define rules on the types that can replace the generic type parameters. They enable developers to enforce type safety while making the code reusable and flexible. Constraints are specified using the <T extends ClassName> syntax or <T extends InterfaceName> where T can only be replaced by a subclass of ClassName or a class implementing InterfaceName.
There are two main types of constraints:
- Upper Bounds: This constraint restricts the type parameter to a specified class or its subclasses. For example,
<T extends Number>allowsTto beInteger,Double, etc., restricting it to subclasses ofNumber. - Lower Bounds: This constraint allows the type parameter to be a specified class or its superclasses, using the wildcard
<T super ClassName>. For example,<T super Integer>acceptsIntegerand any of its superclasses (likeNumberorObject).
Using these constraints allows developers to write more generic, reusable, and type-safe code. They also improve code readability by clearly specifying expectations about the types being used.
Example
import java.util.List;
public class GenericMethod {
public static <T extends Number> void printNumbers(List<T> numbers) {
for (T number : numbers) {
System.out.println(number);
}
}
}
// In the main method, you could call:
// List<Integer> integers = Arrays.asList(1, 2, 3);
// GenericMethod.printNumbers(integers); // prints 1, 2, 3
Common mistakes
- Using primitive types as type parameters, which is not allowed in generics. Always use their corresponding wrapper classes (e.g.,
Integerinstead ofint). - Failing to specify bounds correctly leading to type safety issues.
- Assuming that you can create instances of generic types like
new T(), which is illegal.
Follow-up questions
- Can you explain what wildcards are in Java generics?
- How would you implement a generic class in Java?
- What is type erasure in the context of generics?
Interview tip: Practice writing generic methods and classes to get comfortable with constraints and syntax.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More medium Java questions
- How does the class loader delegation model work in Java, and why does it exist? · 1 ans
- What are the different types of garbage collectors in Java? · 1 ans
- What are the advantages of using interfaces in Java? · 1 ans
- What are static methods and when should you use them? · 1 ans
- What happens when you cast a subclass object to a superclass type? · 1 ans
Related Questions
- How is a lambda different from an anonymous inner class in Java beyond shorter syntax? · Java · 1 ans
- What makes an interface a functional interface, and what does @FunctionalInterface actually do? · Java · 1 ans
- How does HashMap work internally, including collisions, resizing and treeification in Java 8? · Java · 1 ans
- What is the difference between HashSet, LinkedHashSet and TreeSet, and when would you use each? · Java · 1 ans
- How do ON DELETE CASCADE, SET NULL and RESTRICT work, and when are cascades dangerous? · SQL · 1 ans
- What is the difference between a primary key and a unique key in SQL? · SQL · 1 ans
- How do you implement a generic interface and its functions in Java? · Java · 1 ans
- How do abstract classes differ from interfaces in Java? · Java · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)