- Java Generics - Discussion
- Java Generics - Useful Resources
- Java Generics - Quick Guide
- Java Generics - No Overload
- Java Generics - No Exception
- Java Generics - No Array
- Java Generics - No instanceOf
- Java Generics - No Cast
- Java Generics - No Static field
- Java Generics - No Instance
- Java Generics - No Primitive Types
- Java Generics - Methods Erasure
- Unbounded Types Erasure
- Java Generics - Bound Types Erasure
- Java Generics - Types Erasure
- Generics - Guidelines for Wildcards
- Lower Bounded Wildcards
- Generics - Unbounded Wildcards
- Upper Bounded Wildcards
- Java Generics - Generic Map
- Java Generics - Generic Set
- Java Generics - Generic List
- Java Generics - Multiple Bounds
- Bounded Type Parameters
- Java Generics - Raw Types
- Java Generics - Parameterized Types
- Java Generics - Multiple Type
- Java Generics - Generic Methods
- Java Generics - Type inference
- Type Parameter Naming Conventions
- Java Generics - Generic Classes
- Java Generics - Environment Setup
- Java Generics - Overview
- Java Generics - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java Generics - Guidepnes for Wildcard Use
Wildcards can be used in three ways −
Upper bound Wildcard − ? extends Type.
Lower bound Wildcard − ? super Type.
Unbounded Wildcard − ?
In order to decide which type of wildcard best suits the condition, let s first classify the type of parameters passed to a method as in and out parameter.
in variable − An in variable provides data to the code. For example, copy(src, dest). Here src acts as in variable being data to be copied.
out variable − An out variable holds data updated by the code. For example, copy(src, dest). Here dest acts as in variable having copied data.
Guidepnes for Wildcards.
Upper bound wildcard − If a variable is of in category, use extends keyword with wildcard.
Lower bound wildcard − If a variable is of out category, use super keyword with wildcard.
Unbounded wildcard − If a variable can be accessed using Object class method then use an unbound wildcard.
No wildcard − If code is accessing variable in both in and out category then do not use wildcards.
Example
Following example illustrates the above mentioned concepts.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; pubpc class GenericsTester { //Upper bound wildcard //in category pubpc static void deleteCat(List<? extends Cat> catList, Cat cat) { catList.remove(cat); System.out.println("Cat Removed"); } //Lower bound wildcard //out category pubpc static void addCat(List<? super RedCat> catList) { catList.add(new RedCat("Red Cat")); System.out.println("Cat Added"); } //Unbounded wildcard //Using Object method toString() pubpc static void printAll(List<?> pst) { for (Object item : pst) System.out.println(item + " "); } pubpc static void main(String[] args) { List<Animal> animalList= new ArrayList<Animal>(); List<RedCat> redCatList= new ArrayList<RedCat>(); //add pst of super class Animal of Cat class addCat(animalList); //add pst of Cat class addCat(redCatList); addCat(redCatList); //print all animals printAll(animalList); printAll(redCatList); Cat cat = redCatList.get(0); //delete cat deleteCat(redCatList, cat); printAll(redCatList); } } class Animal { String name; Animal(String name) { this.name = name; } pubpc String toString() { return name; } } class Cat extends Animal { Cat(String name) { super(name); } } class RedCat extends Cat { RedCat(String name) { super(name); } } class Dog extends Animal { Dog(String name) { super(name); } }
This will produce the following result −
Cat Added Cat Added Cat Added Red Cat Red Cat Red Cat Cat Removed Red CatAdvertisements