Recall that generic code can have an upper bound when defined using a type parameter or a wildcard. We can also provide a lower bound when working with wildcards. A lower bound wildcard restricts the wildcard to a class or interface and any of its parent types. For example:
public class Util { public static void getBag(Bag<? super Integer> bag ) { return bag; } }
In the example above we’ve used the super
keyword to restrict the argument to getBag()
to be a Bag
of Integer
, Number
, or Object
. If a call to getBag()
with Bag<Double>
was made it would result in an exception because Double
is not an Integer
or one of its parents.
Some important things to note about lower bounds are:
- They cannot be used with generic type parameters, only wildcards.
- A wildcard cannot have both a lower bound and upper bound, in this case, it’s best to use a type parameter.
There are some general guidelines provided by Java as to when to use what type of wildcard:
- An upper bound wildcard should be used when the variable is being used to serve some type of data to our code.
- A lower bound wildcard should be used when the variable is receiving data and holding it to be used later.
- When a variable that serves data is used and only uses
Object
methods, an unbounded wildcard is preferred. - When a variable needs to serve data and store data for use later on, a wildcard should not be used (use a type parameter instead).
Let’s practice adding a lower bound to a generic method.
Instructions
Let’s practice working with “in” and “out” references to understand how to use wildcards with bounds.
In Main.java, transferData()
is currently giving an error on incompatible types as we try to read an object from src
and transfer it to dsc
. Add a bound on src
so that it’s type is bounded to a Student
or any of its sub-classes. Also, add a bound to dsc
so that it’s type is bounded to a SchoolPerson
or any of it’s parent classes.
You should see the contents of dsc
before and after the transfer of data occurs after you make your changes and run the program.