This question might resemble cast the Parent object to Child object in C# but it is about C# and I have a question about Java.
I am also aiming to make a generic builder. The generic builder should only build the parts of the abstract object and the children all manage the induvidual implementations of the abstract class.
abstract class GenericBuilder<B extends GenericBuilder>
{
//lots of build methods
public B lastBuildingMethodInTheChain(Object someValue)
{
//assignment
return this;//<-- is not allowed!
}
}
But when I put in a cast:return (B) this;
it is fine.
The cast is something I want to prevent, but this also restricts the children builders from using their special methods.
The last thing makes sense because the Generic type is only known at runtime, but I have no clue how to write it so that it would work at compile time.
Thank you!