I'm aware that you could call a method from a constructor, but it could lead to problems later on if the method could be overridden. The following code is just an example I've written demonstrating my issue:
Let say I have the following subclass:
private boolean isActive;
public ChargeGun(String gameObjectID, String gameObjectName, String desc, int numberofshots, int damage, boolean isActive) {
super(gameObjectID, gameObjectName, desc, numberofshots, damage);
setAttributes(isActive);
}
public ChargeGun(String gameObjectID, String gameObjectName, int numberofshots, int damage, boolean isActive) {
super(gameObjectID, gameObjectName, numberofshots, damage);
setAttributes(isActive);
}
private final void setAttributes(boolean isActive)
{
this.isActive = isActive;
}
The boolean isActive
is specific to ChargeGun
and not any other gun. Given that I have two constructors in my subclass, rather than type isActive = true
twice, could I call a private final method
to do it? The reason is, if my attributes for ChargeGun grow, then I would have to add attributes in both constructors. If I do it with a private final method, everything is set in one place. Is this bad practice?
I can't chain the ChargeGun constructors because both call the super classes constructor.