- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are upper-bounded wildcard w.r.t Generics method in Java?
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.
To define a generic class you need to specify the type parameter you are using in the angle brackets “<>” after the class name and you can treat this as datatype of the instance variable an proceed with the code.
Example
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); Student<String> std2 = new Student<String>("25"); std2.display(); Student<Integer> std3 = new Student<Integer>(25); std3.display(); } }
Output
Value: 25.5 Value: 25 Value: 25
Wildcards
Instead of the typed parameter in generics (T) you can also use “?”, representing an unknown type. You can use a wild card as a −
- Type of parameter.
- Field
- Local field.
Upper-bounded wildcards
Upper bounds in wild cards is similar to the bounded type in generics. Using this you can enable the usage of all the subtypes of a particular class as a typed parameter.
For example, if want to accept a Collection object as a parameter of a method with the typed parameter as a sub class of the number class, you just need to declare a wild card with the Number class as upper bound.
To create/declare an upper-bounded wildcard, you just need to specify the extends keyword after the “?” followed by the class name.
Example
Following Java example demonstrates the creation of the upper-bounded wildcard.
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.HashSet; public class UpperBoundExample { public static void sampleMethod(Collection<? extends Number> col){ for (Number num: col) { System.out.print(num+" "); } System.out.println(""); } public static void main(String args[]) { ArrayList<Integer> col1 = new ArrayList<Integer>(); col1.add(24); col1.add(56); col1.add(89); col1.add(75); col1.add(36); sampleMethod(col1); List<Float> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f); sampleMethod(col2); HashSet<Double> col3 = new HashSet<Double>(); col3.add(25.225d); col3.add(554.32d); col3.add(2254.22d); col3.add(445.21d); sampleMethod(col3); } }
Output
24 56 89 75 36 22.1 3.32 51.4 82.7 95.4 625.0 25.225 554.32 2254.22 445.21
If you pass a collection object other than type that is subclass of Number as a parameter to the sampleMethod() of the above program a compile time error will be generated.
Example
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.HashSet; public class UpperBoundExample { public static void sampleMethod(Collection<? extends Number> col){ for (Number num: col) { System.out.print(num+" "); } System.out.println(""); } public static void main(String args[]) { ArrayList<Integer> col1 = new ArrayList<Integer>(); col1.add(24); col1.add(56); col1.add(89); col1.add(75); col1.add(36); sampleMethod(col1); List<Float> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f); sampleMethod(col2); HashSet<String> col3 = new HashSet<String>(); col3.add("Raju"); col3.add("Ramu"); col3.add("Raghu"); col3.add("Radha"); sampleMethod(col3); } }
Compile time error
UpperBoundExample.java:31: error: incompatible types: HashSet<String> cannot be converted to Collection<? extends Number> sampleMethod(col3); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error