By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Generic classes and methods in Java use type parameters to create reusable, type-safe code. This topic is crucial for writing flexible and maintainable code. It's heavily tested in Java certification exams. Getting it wrong can lead to runtime errors and less efficient code. For example, without generics, you might need to cast objects manually, risking ClassCastException.
<T>
<T extends Number>
java public class Box<T> { private T content; public void set(T content) { this.content = content; } public T get() { return content; } }
⚠️ Pitfall: Forgetting to use the type parameter in methods can lead to type-unsafe code.
Define a Generic Method
java public static <T> void printArray(T[] array) { for (T element : array) { System.out.println(element); } }
⚠️ Pitfall: Misusing type parameters can lead to confusing and less readable code.
Use Bounded Type Parameters
java public static <T extends Number> double sum(T[] numbers) { double sum = 0; for (T number : numbers) { sum += number.doubleValue(); } return sum; }
⚠️ Pitfall: Overly restrictive bounds can limit the flexibility of your code.
Instantiate and Use Generic Classes
java Box<Integer> integerBox = new Box<>(); integerBox.set(123); System.out.println(integerBox.get());
Box box = new Box();
Experts view generic classes and methods as tools for writing highly reusable and type-safe code. They think in terms of abstraction and flexibility, focusing on how to make their code adaptable to different types without sacrificing type safety.
Exam trap: Questions that trick you into using raw types.
The mistake: Misusing bounded type parameters.
Exam trap: Questions that require understanding of bounded type parameters.
The mistake: Forgetting to use the type parameter in methods.
Exam trap: Questions that test your understanding of type parameters in methods.
The mistake: Overcomplicating generic methods.
Scenario: You need to create a generic class to store and retrieve objects of any type.Question: Write a generic class Container with methods to set and get objects.Solution:
Container
public class Container<T> { private T item; public void setItem(T item) { this.item = item; } public T getItem() { return item; } }
Answer: The Container class can store and retrieve objects of any type.Why it works: The type parameter <T> allows the class to operate on any data type while maintaining type safety.
Scenario: You need to write a generic method to find the maximum value in an array of any type that extends Comparable.Question: Write a generic method max that returns the maximum value in an array.Solution:
Comparable
max
public static <T extends Comparable<T>> T max(T[] array) { T max = array[0]; for (T element : array) { if (element.compareTo(max) > 0) { max = element; } } return max; }
Answer: The max method finds the maximum value in an array of any type that extends Comparable.Why it works: The bounded type parameter <T extends Comparable<T>> ensures that the elements can be compared.
<T extends Comparable<T>>
?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.