In my study book, there’s this example:
import java.util.*;
public class RentalGeneric<T> {
private List<T> rentalPool;
private int maxNum;
public RentalGeneric(int maxNum, List<T> rentalPool) {
this.maxNum = maxNum;
this.rentalPool = rentalPool;
}
public T getRental() {
return rentalPool.get(0);
}
public void returnRental(T returnedThing) {
rentalPool.add(returnedThing);
}
}
I find it strange that it compiles, since there is no definition of Class<T>. What is the story about this? It says in my book that T is for the type parameter but how do I know when to use it?
You could use it to rent cars, bikes …
You could use it directly like this:
Then when you’ll do
getRentalit’ll return you aCarobject.And you’ll be able to put back a
CarwithreturnRental(aCar);Or you could create a
CarRentalclass extendingRentalGeneric<Car>.Same thing goes for whatever object you would like to rent.