Transferred from: Http://onewebsql.com/blog/generics-extends-super
The following are basically enough
Today We continue our mini-series on Java generics. In previous posts we have investigated
- How to crash your IDE using Java wildcards,
- What beginners find surprising when creating parametrized objects, and
- What is the difference between Java arrays and generic lists.
Java type parameters is used as type placeholders.
?
A list<x> is a container for X objects, and x can being instantiated with any class:you can Havelist<object>, list<string>, and list<number>.
Introducing Bounds:extends
You often want to restrict the set of types the can is used in instantiation. If you create a class Garage, you want it to the hold only Vehicle objects.
The syntax you use in Java are like this:
?
1 |
public class Garage<X extends Vehicle> { } |
Every time you instantiate the Garage, the type parameter have to be a subclass of Vehicle.
?
123 |
class Car extends Vehicle { } class Motorcycle extends Vehicle { } class Fruit extends Object { } |
Thus garage<car> and garage<motorcycle> are OK but garage<fruit> is Not OK.
You can specify more than one bound with extends:
?
1234 |
class Vehicle { } interface PassengerVehicle { } interface MotorVehicle { } class ParkingGarage<X extends Vehicle & MotorVehicle & PassengerVehicle> |
You can specify at the very one class in the bound (obviously, as can only inherit from one class in Java) and As many interfaces as you want.
The bound can refer to the typing parameter:
?
1 |
class BST<X extends Comparable<X>> {} |
BST class can only is instantiated with classes X which implement the comparable<x>interf Ace.
Bounds in method parameters
Java method can be parametrized, too. The syntax is as follows:
?
1 |
<T> T getRandomElement(List<T> list) {} |
As with class definitions, you often want-restrict the type parameter in the method. A method which takes a list of Vehicles and returns the fastest Vehicle in the list can has the following type.
?
1 |
<T extends Vehicle> T getFastest(List<T> list) { } |
You can pass as argument a list of any vehicles. List<car> is OK, list<motorcycle> are OK, list<vehicle> is OK, too. List<number> is not OK.
Note that the following declaration wouldn ' t does the trick.
?
1 |
Vehicle getFastest2(List<Vehicle> list) { } |
The argument to the method GetFastest2 have to is exactly a list<vehicle>, and not aList<car >, because list<car> is not a subtype of list<vehicle>,
Wilcards
Take a look at the following declaration.
?
1 |
<T extends Vehicle> int totalFuel(List<T> list) { } |
The parameter T occurs only once in the method signature, in an argument. You can imagine this method body does not use the name T either. In this case can be use of an alternative syntax, called wildcards, denoted with ?:
?
1 |
int totalFuel(List<? extends Vehicle> list) { } |
The signatures for Totalfuel is equivalent. The meaning of <? extends vehicle> is:i don ' t care what the type parameter are, as long as it is a subclass of Vehicle.
Introducing Bounds:super
There is also dual bound, called Super. As you guess it's used to denote so can pass only superclasses of the The bound. There is some differences between extends and Super, though.
You can ' t use Super in class declaration
The super bound is not a allowed in class definition.
?
12 |
//this code does not compile ! class Forbidden<X super Vehicle> { } |
Why? Because such construction doesn ' t make sense. For example, your can ' t erase the type parameter with Vehicle because the class Forbidden could is Instan tiated with Object. So you had to erase type parameters to Object anyway. If think about class forbidden<object>, it can take any value in place of X, not only Supercla SSEs of Vehicle. There ' s no point in using Super bound, it wouldn ' t get us anything. Thus it is not allowed.
Wildcards
The syntax for wildcards are also similar to extends:
?
1 |
int totalValue(Valuer<? super Vehicle> valuer) |
The method has to take a comparator which are able to compare VehicleS. If It comparesObjects as well, that ' s fine too.
When to use extends and super
Wildcards is most useful in method parameters. They the necessary flexibility in method interfaces.
People is often confused when to use extends and when to use super bounds. The rule of thumb is the get-put principle. If you get the something from a parametrized container, useextends.
?
1234567 |
int totalfuel (list<? extends vehicle> list" { &NBSP;&NBSP;&NBSP;&NBSP; int total = 0 ; &NBSP;&NBSP;&NBSP;&NBSP; for (Vehicle v:list) { total + = V.getfuel (); &NBSP;&NBSP;&NBSP;&NBSP; } &NBSP;&NBSP;&NBSP;&NBSP; return total; |
The method Totalfuel gets Vehicles from the list, asks them on how much fuel they has, and Computes the total.
If you put objects to a parametrized container, use super.
?
1234567 |
int totalvalue (valuer<? super vehicle> valuer) { &NBSP;&NBSP;&NBSP;&NBSP; int total = 0 &NBSP;&NBSP;&NBSP;&NBSP; for (Vehicle v:vehicles) { total + = Valuer.evaluate (v); &NBSP;&NBSP;&NBSP;&NBSP; } &NBSP;&NBSP;&NBSP;&NBSP; return total; } |
The method Totalvalue puts Vehicles into the valuer.
It's useful to know that extends bound are much more common than super.
One more tip:if-intimidated by wildcards (which was natural in the beginning), try to write the explicitly paramet rized version first. In typical usage the versions is equivalent. Eventually, you'll figure out when you can get rid of the type parameters and use wildcards.
about how Java generics are used ....