I have just learnt how to use generics and i understand the syntax. However i want to know when to use them so that i do not just throw generics everywhere just because i can. You can use the example below to assist in the explanation:
//First I have:
public class GenerateTemplate<T> {
public GenerateTemplate(T template) {
this.template = template;
}
private T template;
public T getTemplate() {
return Template;
}
}
//Second i have:
public abstract class TemplateGenerator {
public abstract String templateName();
public void readTemplate(){
System.out.println("Reading Template);
}
}
//Third i have:
public class ExcelTemplate extends TemplateGenerator {
@Override
public String templateName() {
System.out.println("I am excel template);
return null;
}
//Main below works works well on both occasions but what is the best practice there and why?
public static void main(String[] args) {
final TemplateGenerator templateGenerator = new ExcelTemplate ();
templateGenerator.readTemplate();
ExcelTemplate template = new ExcelTemplate();
final GenerateTemplate<ExcelTemplate> generatorTemplate = new
GenerateTemplate(template);
generatorTemplate.getTemplate().readTemplate();
}