0

I'm using this constructor in a java class called PlayableFighter

public PlayableFighter(String name, int maxHealthPoints, int blastDamage, int physicalDamage,
        int maxKi, int maxStamina, ArrayList<SuperAttack> superAttacks, ArrayList<UltimateAttack>
        ultimateAttacks)

and I want to call it from a class called Earthling which inherits PlayableFighter but I don't have the values of superAttacks or ultimateAttacks so I would like to set them to default value, I used null but is there a better way?

public Earthling(){
super("Earthling",1250,50,50,4,4,null,null);
}
Lose' CKi
  • 31
  • 6

2 Answers2

1

If you can manage the code of PlayableFighter i would recommend adding another constructor that gets all of the parameters except "superAttacks" and "ultimateAttacks" that will call the other constructor with default parameters, like this:

public PlayableFighter(String name, int maxHealthPoints, int blastDamage, int physicalDamage, int maxKi, int maxStamina){
this(name, maxHealthPoints, blastDamage, physicalDamage, maxKi, maxStamina, null, null);
}
eladyanai
  • 1,063
  • 2
  • 15
  • 34
0

I don't like seeing nulls tossed around this way. An object should be 100% ready to go when you create it.

I'd do it this way:

public Earthling() {
    super("Earthling",1250,50,50,4,4,new ArrayList<SuperAttack>(), new ArrayList<UltimateAttack>());
}

You aren't using inheritance as well as you might. I'd have an Attack interface with subclasses UltimateAttack and SuperAttack.

Your naming is bad. Who would have any idea what the difference between those two is?

Why do you have to pass "Earthling" label to an Earthling class? More silliness.

What are all those magic numbers you pass in the default constructor to the super constructor? More silliness.

duffymo
  • 305,152
  • 44
  • 369
  • 561