I saw this pattern somewhere:
class A extends B {
}
This structure is a little unusual to extend a generic by specifying the new
Of course the OOP answer is that A is a B. If A were not a B than A should merely compose itself with a B to make use of B's functionality.
Presumably B also has some general implementations which take advantage of restrictions placed on the generic type.
Another use case would be for B to look something like:
abstract class B> {
public T createCopy(T t);
}
Now subclasses can implement createCopy and client code can safely use it without having to cast... e.g.
class A extends B {
public A createCopy(A t) {
return new A(t); //copy constructor
}
}
Compare the above to:
abstract class B {
public B createCopy(B t);
}
class A extends B {
public B createCopy(B t) { //Is the copy an A or a different subtype of B? We don't know.
return new A(t); //copy constructor
}
}