Java: Why is this Subclass valid?

Posted by incrediman on Stack Overflow See other posts from Stack Overflow or by incrediman
Published on 2010-03-29T02:07:02Z Indexed on 2010/03/29 2:13 UTC
Read the original article Hit count: 572

Filed under:
|
|

Here, I have an abstract class:

abstract class A<E extends A> {
    abstract void foo(E x);
}

Here's a class that extends A:

class B extends A<B>{
    void foo(B x){}
}

And here's another (E is B here on purpose):

class C extends A<B>{
    void foo(B x){}
}

Both of those classes are valid, and the reasoning for that makes sense to me.

However what confuses me is how this could possibly be valid:

class D extends A{
    void foo(A x){}
}

Since when are generics optional like that? I thought the extending class (subclass) of A would be required to specify an E?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics