1. Home
  2. Interview Questions
  3. Java
  4. 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

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best 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> allows T to be Integer, Double, etc., restricting it to subclasses of Number.
  • 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> accepts Integer and any of its superclasses (like Number or Object).

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., Integer instead of int).
  • 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

All medium questions →

Related Questions

‹
Q12 of 18 · Java
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA