I am currently learning generics and having a hard time comprehending some aspects of them. I feel like I've overlooked something so the question might sound stupid.
I understand that there are:
- Placeholders, known as 'formal type parameters'.
- Actual 'type arguments'.
Here's a piece of sample code I have with generics and method chaining that works:
class Clothing <T> {
String material;
String color;
T setMaterial (String material) {
this.material = material;
return (T) this;
}
T setColor (String color) {
this.color = color;
return (T) this;
}
}
class Jeans extends Clothing <Jeans> {
}
class Pants extends Clothing <Pants> {
}
class Executor {
public static void main(String[] args){
Jeans jeansPair = new Jeans().setMaterial("cotton").setColor("green");
}
}
The problem is I don't understand why the type arugment such as Jeans and Pants are provided in sub-class declarations instead of instantiations like the one in the main method.
I would appreciate if you provided a link to this rule--I've looked up a lot of info such as bounded parametrs, raw types, erasure etc. but did not quite find what I was looking for.
Thanks