Home > Software engineering >  How to properly change value of a field that exist in parent and child object
How to properly change value of a field that exist in parent and child object

Time:09-15

So i have a parent class Warrior and a Knight class that extends Warrior. Instructions in tasks says Warrior has 2 fields:

  • int health = 50,
  • int attack = 5

Knight must have attack amount of 7. How to properly change Knight "attack" field without breaking SOLID rules etc. My code looks like that rn.

public class Warrior {

    private int health;
    private int attack;

    public Warrior() {
        this.health = 50;
        this.attack = 5;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

-----------------------------------------------

    public class Knight extends Warrior {

    public Knight() {
        super.setAttack(7);
      }
    }

And it works just as i want but i think there is a better solution that i cannot find. Can i just change modificator of attack field on public and then go for super.attack?

CodePudding user response:

You can make a no-args constructor for your Warrior class that calls an overloaded constructor with this(50, 5). You can then use super(50, 7) in your Child class of Knight to call the same constructor, but with different values.

You could also add an overloaded option for only attack as a parameter like super(7) if you really do not want to pass the health of a Knight when it is getting made:

public class Warrior {

    private int health;
    private int attack;

    public Warrior() {
        this(50, 5);
    }
    
    public Warrior(int health, int attack) {
        this.health = health;
        this.attack = attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }
}

//Separate Java File
public class Knight extends Warrior {
    public Knight() {
        super(50, 7);
    }
}

I would use something like setAttack if you need to change the value after the object is created.

CodePudding user response:

When you need several of objects having the same behavior and set of properties, it's unnecessary to create a separate class for each of them.

You can create as many instances as required, having only one class.

If you want these objects to have a fixed set of properties (or only part of the properties to be fixed, and others provided while instantiating the class) you can create an enum, which constants would represent all the kinds of this object you need like WARRIOR, KNIGHT, etc.

Each enum-constants can maintain a fixed set of properties that correspond to one of the specific flavors of your object.

For example:

public enum FighterType {
    WARRIOR(5), KNIGHT(7);
    
    private int attack; // add other properties, like health if needed

    FighterType(int attack) {
        this.attack = attack;
    }

    // getters
}

And that how your single base class might look like:

public class Fighter {
    private String name;        // provided in the client code while creating a new instance
    private int health = 50;    // if you want this property to be the same for every instance, there's no need to assign it inside the constructor
    private int attack;         // comes with the type
    private FighterType type;   // WARRIOR, KNIGHT, etc.

    public Fighter(FighterType type, String name) {
        this.name = name;
        this.type = type;
        this.attack = type.getAttack();
    }

    // getters, etc.
}

Usage example:

Fighter richard = new Fighter(FighterType.KNIGHT, "Richard The Lionhearted");
  • Related