I'm looking over some code in a game and I came across something that I haven't seen before and I don't really know whats going on.
public abstract class Entity
{
public Entity(World world)
{
// irrelevent code
entityInit();
}
protected abstract void entityInit();
}
What's going on here? What happens when it calls on entityInit()
?
An abstract class is never instantiated. Only its concrete subclasses can be instantiated. So, when the concrete subclass (let's call it Foo
) constructor is called, it calls super(world)
. The Entity constructor then calls entityInit()
, which has been overridden by Foo
. It thus calls the Foo entityInit
concrete method.
Note that this is bad practice, because the entityInit
method will be called on a not yet fully constructed object. The subclass thus has to make sure this method doesn't access any field it might declare, because they will all be unititialized.