Unbounded Wildcard

  • The question mark (?), represents the wildcard, stands for unknown type in generics.
  • There may be times when any object can be used when a method can be implemented using functionality provided in the Object class or When the code is independent of the type parameter.
  • To declare a Unbounded Wildcard parameter, list the ? only.

Example

package com.test.unbounded;

import java.util.Arrays;
import java.util.List;

public class UnboundedDemo {
	public static void printList(List<?> list) {
		System.out.println();
	      for (Object item : list)
	         System.out.print(item + " --> ");
	   }

	   public static void main(String args[]) {
	      printList(Arrays.asList(1, 2, 3));
	      printList(Arrays.asList(1.2, 2.3, 3.5));
	      printList(Arrays.asList("Tyson","Justin","Martin"));
	   }
}

Upper 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 Number or its subclasses.
  • To declare a upper bounded Wildcard parameter, list the ?, followed by the extends keyword, followed by its upper bound.
  • If we want to read values from a collection then it is better to use extends
package com.test.unbounded;

import java.util.Arrays;
import java.util.List;

public class UpperBoundDemo {
	public static double sum(List<? extends Number> numberlist) {
		double sum = 0.0;
		for (Number n : numberlist)
			sum += n.doubleValue();
		return sum;
	}

	public static void main(String args[]) {
		System.out.println("sum = " + sum(Arrays.asList(1, 2, 3)));
		System.out.println("sum = " + sum(Arrays.asList(1.2, 2.3, 3.5)));
		//Below will throw a compile time error
		//System.out.println("sum = " + sum(Arrays.asList("Tyson","justin","Martin")));
	}
}

Lower Bound 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.
  • If we want to add values to the collection it is better if we use super

Example :

package com.test.unbounded;

import java.util.ArrayList;
import java.util.List;

public class LowerBoundDemo {
	 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 {}

Note : Producer and Consumer problem can be implemented using the upper and lower bound wild cards.Extends

Leave a Comment