Upper Bounded Type Parameters
An upper bound restricts a type parameter to a specific class or interface, or any of its subtypes. In generic type declarations, theextends keyword is used universally to denote an upper bound, regardless of whether the bounding type is a class or an interface.
Multiple Bounds (Intersection Types)
A type parameter can be constrained by multiple bounds using the& operator. The generic type argument must be a subtype of all specified bounds.
Due to Java’s single inheritance model, a multiple bound can contain at most one class. If a class is specified, it must be the first bound in the intersection type, followed by any number of interfaces.
Distinction: Type Parameters vs. Bounded Wildcards
According to the Java Language Specification, type parameters are distinct from wildcards. Type parameters are the variables declared in a generic class or method signature (e.g., theT in class Box<T>) and can only accept upper bounds.
Wildcards (?), conversely, are used as type arguments in parameterized types (e.g., the ? in List<?>). Unlike type parameters, wildcards support both upper and lower bounds to establish covariance and contravariance.
Upper Bounded Wildcards (? extends Type)
Establishes covariance. It represents an unknown type argument that is a specific type or a subtype of that type.
? super Type)
Establishes contravariance. It represents an unknown type argument that is a specific type or a supertype of that type. Lower bounds use the super keyword and cannot be applied to standard type parameters.
Type Erasure and Bounds
During compilation, Java applies type erasure to generic parameters. If a type parameter is bounded, the compiler replaces the type parameter with its first bound. If there are multiple bounds, the compiler inserts necessary type casts for methods belonging to the subsequent bounds.Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More





