By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Bounded type parameters using the extends keyword in Java are a crucial concept in generic programming. They allow you to restrict the types that can be used as type arguments in a parameterized type. This is vital for creating flexible and reusable code while maintaining type safety. In real-world applications, bounded type parameters help prevent runtime errors by enforcing compile-time checks. For example, if you mistakenly use an incompatible type, the compiler will catch the error, saving you from potential bugs and crashes. Understanding this concept is essential for Java certification exams and professional development.
? extends Number
extends
java public class Box<T extends Number> { private T value; public void set(T value) { this.value = value; } public T get() { return value; } }
⚠️ Pitfall: Using a class that does not extend the specified bound will result in a compile-time error.
Use the Bounded Type Parameter in Methods
java public class Util { public static <T extends Number> void printValue(T value) { System.out.println(value); } }
⚠️ Pitfall: Attempting to use methods that are not available in the bounding type will cause errors.
Apply Wildcards for Flexible Bounds
java public static void printList(List<? extends Number> list) { for (Number n : list) { System.out.println(n); } }
⚠️ Pitfall: Misusing wildcards can lead to unintended type restrictions.
Understand Type Erasure
Object
Experts view bounded type parameters as a tool for balancing flexibility and type safety. They think in terms of type hierarchies and method contracts, ensuring that their generic types are both versatile and robust. Instead of memorizing specific bounds, they consider the behavior and relationships between types, using bounds to enforce logical constraints.
Exam trap: Questions may trick you with types that seem compatible but are not.
The mistake: Attempting to use methods not available in the bounding type.
Exam trap: Watch for methods that are specific to subtypes but not the bounding type.
The mistake: Misusing wildcards in method signatures.
? extends
? super
Exam trap: Questions may test your understanding of wildcard bounds.
The mistake: Relying on type information at runtime.
Scenario: You need to create a generic class that can store any type of number but not other types.Question: How would you define this class? Solution:1. Define a generic class with a type parameter that extends Number.2. Use the type parameter in the class methods.Answer:
Number
public class NumberBox<T extends Number> { private T value; public void set(T value) { this.value = value; } public T get() { return value; } }
Why it works: The extends Number bound restricts the type parameter to number types, ensuring type safety.
extends Number
Scenario: You need to write a method that prints a list of any type of number.Question: How would you define this method? Solution:1. Use a wildcard with an upper bound in the method signature.2. Iterate through the list and print each element.Answer:
public static void printList(List<? extends Number> list) { for (Number n : list) { System.out.println(n); } }
Why it works: The wildcard ? extends Number allows the method to accept lists of any number type.
Scenario: You need to create a method that adds elements to a list of any type that extends Number.Question: How would you define this method? Solution:1. Use a wildcard with a lower bound in the method signature.2. Add elements to the list.Answer:
public static void addNumbers(List<? super Number> list, Number... numbers) { for (Number n : numbers) { list.add(n); } }
Why it works: The wildcard ? super Number allows the method to add elements to lists of any type that is a supertype of Number.
? super Number
<T extends Number>
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.