I am experimenting with Java generics. I understand with Java generics we can create class and methods which only deals with specific types. This enable detection of programming error at compile time.
My question may sounds strange. Why use E extends Superclass instead of Superclass? Please take a look at the code below.
class Hunter <E extends Animal>{}
class Keaper <Animal>{}
/*edit: as AR.3 and other pointed out, Keaper<Animal> is the same as Keaper<E>.
Animal is just a type parameter here*/
class Animal{}
class Cat extends Animal{}
class Dog extends Animal{}
public class TestGeneric {
public static void main(String[] args) {
Hunter<Cat> hunter1 = new Hunter<>();
Hunter<Dog> hunter2 = new Hunter<>();
Hunter<Animal> hunter3 = new Hunter<>();
ArrayList<Hunter> hunters= new ArrayList<>();
hunters.add(hunter1);
hunters.add(hunter2);
hunters.add(hunter3);
Keaper<Cat> keaper1 = new Keaper<>();
Keaper<Dog> keaper2 = new Keaper<>();
Keaper<Animal> keaper3 = new Keaper<>();
//Edit: as AR.3 and others pointed out, Keaper<String> is also legal here.
ArrayList<Keaper> keapers= new ArrayList<>();
keapers.add(keaper1);
keapers.add(keaper2);
keapers.add(keaper3);
}
}
I feel E extends Animal is almost the same as Animal, except the first one may provide more information. Any idea?