I want to call methods defined in the parent class that return "this" but I want their return Type to be the child type, not the parent type.
Currently I am using this ugly, inherently error prone technique, where mistakes would only become visible through ClassCast Exceptions...
abstract class AbstractClass<T extends AbstractClass<?>>{
public T doSomething(){
return (T)this;
}
}
class Implementation extends AbstractClass<Implementation>{
}
// usage
Implementation impl = new Implementation().doSomething().doSomething();
Is there any smarter way to do this?
Does my way (Casting to generic Type) reduce performance compared to implementing a concrete return type in each child class?