Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
While generic classes parameterize an entire class, generic methods introduce their own type parameters scoped solely to that specific method. Furthermore, when you need to restrict the allowable types to subclasses of a specific class or implementors of an interface, Java provides Bounded Type Parameters using the extends keyword.
A generic method declares its type parameter(s) before the method's return type:
<T extends SuperClass>)Sometimes an algorithm requires knowledge about the capabilities of T. For example, calculating numeric statistics requires mathematical operations, which are available on subclasses of java.lang.Number.
<T extends Class & Interface>)In Java, a type parameter can have multiple bounds. If one of the bounds is a class, it must be listed first, followed by interface bounds separated by &:
int or double directly in generics (StatsCalculator<int> ❌). You must use wrapper classes (StatsCalculator<Integer> ✅).<T extends Serializable & Number> ❌ compile error; <T extends Number & Serializable> ✅ correct.Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 1 questions. Each question has one correct answer.