What are the guide lines to be followed while using wildcards in Java?


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. There are two types of wildcards in Java −

  • 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.

  • Lower-Bounded wildcards − 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.

Guide lines to be followed

While using wildcards in Java you need to keep the following points in mind.

  • If you (your method) have a variable that gets the data (from which you read the data), you need to define it with upper-bounded wildcard (extends).

  • If you (your method) have a variable that holds the data (into which you read the data), you need to define it with lower-bounded wildcard (super).

  • If you (your method) need to invoke the methods of the Object class using the variable that gets data, in such scenarios you need to use an unbounded wild card.

  • If you (your method) need to use both variables that read and store the data, you need not use the wildcard.

Example

 Live Demo

import java.util.ArrayList;
import java.util.List;
   class Person {
   String name;
   Person(String name) {
      this.name = name;
   }
   public String toString() {
      return name;
   }
}
class Employee extends Person {
   Employee(String name) {
      super(name);
   }
}
class NonTeachingStaff extends Employee {
   NonTeachingStaff(String name) {
      super(name);
   }
}
class Student extends Person {
   Student(String name) {
      super(name);
   }
}
public class WildCardGuidelines {
   //Upper bound wildcard
   //in category
   public static void deleteEmp(List<? extends Employee> empList, Employee emp) {
      empList.remove(emp);
      System.out.println("emp Removed");
   }
   //Lower bound wildcard
   //out category
   public static void addEmp(List<? super NonTeachingStaff> empList) {
      empList.add(new NonTeachingStaff("nts"));
      System.out.println("emp Added");
   }
   //Unbounded wildcard
   //Using Object method toString()
   public static void printAll(List<?> list) {
      for (Object item : list)
         System.out.println(item + " ");
   }
   public static void main(String[] args) {
      List<Person> personList= new ArrayList<Person>();
      List<NonTeachingStaff> ntsList= new ArrayList<NonTeachingStaff>();
      addEmp(personList);
      addEmp(ntsList);
      addEmp(ntsList);
      //print all data
      printAll(personList);
      printAll(ntsList);
      Employee emp = ntsList.get(0);
      deleteEmp(ntsList, emp);
      printAll(ntsList);
   }
}

Output

emp Added
emp Added
emp Added
nts
nts
nts
emp Removed
nts
raja
Published on 12-Sep-2019 08:22:22
Advertisements