
- Generics - Home
- Generics - Overview
- Generics - Environment Setup
- Examples - Generic Classes
- Generics - Generic Classes
- Type Parameter Naming Conventions
- Generics - Type inference
- Generics - Generic Methods
- Generics - Multiple Type Parameters
- Generics - Parameterized Types
- Generics - Raw Types
- Examples - Bounded Type Parameters
- Bounded Type Parameters
- Generics - Multiple Bounds
- Examples - Collections
- Generics - Generic List
- Generics - Generic Set
- Generics - Generic Map
- Examples - Wild Cards
- Upper Bounded Wildcards
- Generics - Unbounded Wildcards
- Lower Bounded Wildcards
- Generics - Guidelines for Wildcards
- Type Erasure
- Generics - Generic Types Erasure
- Generics - Bound Types Erasure
- Unbounded Types Erasure
- Generics - Generic Methods Erasure
- Restrictions on Generics
- Generics - No Primitive Types
- Generics - No Instance
- Generics - No Static field
- Generics - No Cast
- Generics - No instanceOf
- Generics - No Array
- Generics - No Exception
- Generics - No Overload
- Generics Useful Resources
- Generics - Quick Guide
- Generics - Useful Resources
- Generics - Discussion
Java Generics - Lower Bounded Wildcards
The question mark (?), represents the wildcard, stands for unknown type in generics. There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Integer or its superclasses like Number.
To declare a lower bounded Wildcard parameter, list the ?, followed by the super keyword, followed by its lower bound.
Example
Following example illustrates how super is used to specify an lower bound wildcard.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class GenericsTester { public static void addCat(List<? super Cat> catList) { catList.add(new RedCat()); System.out.println("Cat Added"); } public static void main(String[] args) { List<Animal> animalList= new ArrayList<Animal>(); List<Cat> catList= new ArrayList<Cat>(); List<RedCat> redCatList= new ArrayList<RedCat>(); List<Dog> dogList= new ArrayList<Dog>(); //add list of super class Animal of Cat class addCat(animalList); //add list of Cat class addCat(catList); //compile time error //can not add list of subclass RedCat of Cat class //addCat(redCatList); //compile time error //can not add list of subclass Dog of Superclass Animal of Cat class //addCat.addMethod(dogList); } } class Animal {} class Cat extends Animal {} class RedCat extends Cat {} class Dog extends Animal {}
This will produce the following result −
Cat Added Cat Added
Advertisements