With an abstract class I want to define a method that returns "this" for the subclasses:
public abstract class Foo {
...
public <T extends Foo> T eat(String eatCake) {
...
return this;
}
}
public class Eater extends Foo {}
I want to be able to do things like:
Eater phil = new Eater();
phil.eat("wacky cake").eat("chocolate cake").eat("banana bread");
Foo<T extends Foo<T>>
. This works, though you can easily make a class that will blow up with aClassCastException
when you calleat
, such asclass Bar extends Foo<CakeEater>
. – Statism