Just when I thought I finally understood generics, I came across the following example:
public class Organic<E> {
void react(E e) { }
static void main(String[] args) {
//1: Organic<? extends Organic> compound = new Aliphatic<Organic>();
//2: Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
compound.react(new Organic());
compound.react(new Aliphatic());
compound.react(new Hexane());
} }
class Aliphatic<F> extends Organic<F> { }
class Hexane<G> extends Aliphatic<G> { }
It says, if line 1 is uncommented, the following will not compile:
compound.react(new Organic());
compound.react(new Aliphatic());
compound.react(new Hexane());
while if line 2 is ucommented, the following will not compile:
compound.react(new Organic());
In the second example, Aliphatic and it's supertypes are allowed. So why isn't Aliphatic allowed?
In the first example, why isn't new Organic
allowed??
1st compiler error:
- The method react(capture#1-of ? extends Organic) in the type Organic<capture#1-of ? extends Organic> is not applicable for the arguments (Organic)
- The method react(capture#2-of ? extends Organic) in the type Organic<capture#2-of ? extends Organic> is not applicable for the arguments (Aliphatic)
- The method react(capture#3-of ? extends Organic) in the type Organic<capture#3-of ? extends Organic> is not applicable for the arguments (Hexane)
2nd compiler error:
- The method react(capture#1-of ? super Aliphatic) in the type Organic<capture#1-of ? super Aliphatic> is not applicable for the arguments (Organic)