What are lower-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

 Live Demo

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.

The only restriction on wilds cards is that you cannot it as a type argument of a generic method while invoking it.

Java provides 3 types of wild cards namely upper-bounded, lower-bounded, un-bounded.

Lower-Bounded wildcards

Upper-bounded wildcard enables the usage of all the subtypes of a particular class as a typed parameter.

Similarly, if we use the lower-bounded wildcards you can restrict the type of the “?” to a particular type or a super type of it.

For example, if want to accept a Collection object as a parameter of a method with the typed parameter as a super class of the Integer class, you just need to declare a wildcard with the Integer class as lower bound.

To create/declare a lower-bounded wildcard, you just need to specify the super keyword after the “?” followed by the class name.

Example

Following Java example demonstrates the creation of the Lower-bounded wildcard.

 Live Demo

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
public class LowerBoundExample {
   public static void sampleMethod(Collection<? super Integer> col){
      Iterator it = col.iterator();
      while (it.hasNext()) {
         System.out.print(it.next()+" ");
      }
      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<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
      sampleMethod(col2);
   }
}

Output

24 56 89 75 36
22.1 3.32 51.4 82.7 95.4 625.0

If you pass a collection object other of type other than Integer and its super type as a parameter to the sampleMethod() of the above program a compile time error will be generated.

Example 

Live Demo

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;
public class LowerBoundExample {
   public static void sampleMethod(Collection<? super Integer> col){
      Iterator it = col.iterator();
      while (it.hasNext()) {
         System.out.print(it.next()+" ");
      }
      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<Object> 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);
   }
}

Compile time error

LowerBoundExample.java:34: error: incompatible types: HashSet<Double> cannot be converted to Collection<? super Integer>
      sampleMethod(col3);
                  ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

Updated on: 12-Sep-2019

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements