Definition of a Simple Generic Class
public class Aken<T>{
private T first;
priavte T second;
public Aken(){
first=null;
second=null;
}
public T getFirst(){
return first;
}
public T getsSecond(){
return second;
}
public void setFirst(T first){
this.first=first;
}
public void setSecond(T second){
this.second=second;
}
}
in order to restrict the T Type we can create Class as follow:
class AkenSupport<T extends List>{
private T first;
private T second;
public AkenSupport(){
first=null;
second=null;
}
public T getFirst(){
return first;
}
public T getsSecond(){
return second;
}
public void setFirst(T first){
this.first=first;
}
public void setSecond(T second){
this.second=second;
}
public static void main(String[] args) {
Aken<String> aken=new Aken<String>();
aken.setFirst("hello");
aken.setSecond("world");
}
}
and you may need more bounds you can :
class AkenSupport1<T extends List & Set>{
}
In summary,you need to remember these facts about translation of java generics;
There are no generics in the virtual machines,only ordinary classes and methods
All type parameters are replaced by their bounds;
Bridge methods are synthesized to preserve polymorplism
Casts are inserted as necessary to preserve type safety
Type Parameters Cannot Be Instantiated with Primitive Types
You Cannot Throw or Catch Instances of a Generic Class
You cannot Instantiate Type Variables
Type Variables are not valid in static contexts of generic classes
Wildcard Types
Aken<? extends ArrayList>