- Generic Methods
- Wild Cards (?)
- Upper bounded wildcards
- Unbounded wildcards
- Lower bounded wildcards
Generic Methods :
- Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type at class level, but the type parameter’s scope is limited to the method where it is declared.
- Static and non-static generic methods are allowed, as well as generic class constructors.
- Syntax : The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method’s return type. For static generic methods, the type parameter section must appear before the method’s return type.
public class GenericMethodDemo { //Syntax public void methodOne(ArrayList list){ //This method allows as argument only ArrayList of any object type. } //Syntax public void methodTwo(X arg){ //This method allows as argument any object type. } //Syntax public <K,V> void methodThree(Map<K, V> map){ //This method allows as argument a map of any key and value object types } //Syntax for static methods public static <K,V> void methodFour(Map<K, V> map){ //This method allows as argument a map of any object type as key and any object type as value. } public static void main(String[] args) { GenericMethodDemo demo = new GenericMethodDemo(); ArrayList list1 = new ArrayList(); list1.add("Satish"); demo.methodOne(list1);//calling methodOne demo.methodTwo("Satish");//calling methodTwo Map<Integer, String> namesMap = new HashMap<Integer, String>(); namesMap.put(1, "satish"); demo.methodThree(namesMap);//calling methodThree methodFour(namesMap);//calling methodFour } }
Wild Cards (?) :
- n generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type.
Types are:
- Upper bounded wildcards.
- Unbounded wildcards.
- Lower bounded wildcards.
1. Upper bounded wildcards
To declare an upper-bounded wildcard, use the wildcard character (‘?‘), followed by the extends keyword, followed by its upper bound:<? extends A>
.
//Upper bounded wildcard syntax public void methodUpperBounded(ArrayList<? extends X> list){ /* */ }
- X can be a class or interface.
- If X is a class then as a type parameter we can use either X type or its child classes.
- If X is interface then as a type parameter we can use either X type or its implementation class Type.
- In this case we are allow to add we are not allow to add anything except null with in the method.
2. Unbounded wildcards
The unbounded wildcard type is specified using the wildcard character (?), for example, List<?>
.
//unbounded wildcard syntax public void methodUnBounded(ArrayList<?> list){ /* */ }
This method is applicable for ArrayList of any type but with in the method we are not allow to add anything except null.
3. Lower bounded wildcards
A lower bounded wildcard is expressed using the wildcard character (‘?‘), following by the super keyword, followed by its lower bound: <? super A>
.
//Upper bounded wildcard syntax public void methodLowerBounded(ArrayList<? super X> list){ /* */ }
- X can be a class or interface.
- If X is a class then as a type parameter we can use either X type or its super (parent) classes.
- If X is interface then as a type parameter we can use either X type or its super classes of its implementation class Type X (but not for implementation classes).
- In this case we are allow to add ‘X’ type objects and null with in the method.