0

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();

}   
Etch
  • 191
  • 3
  • 9
  • @Logan the examples i see boil down to casting being the issue. But in example above you will notice no casting was done there but the implementation is still different – Etch Jun 10 '18 at 22:49
  • 1
    This really isn't a duplicate, but this question is just unfortunately too broad to answer reasonably. – Makoto Jun 10 '18 at 23:12

0 Answers0