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);
}
2
  • Constructor Overloading Commented Feb 23, 2016 at 12:16
  • No, there's no better way unless you create one. They are both List type, so you could easily pass an empty List. I'd change the parameter type to prefer the interface. Commented Feb 23, 2016 at 12:17

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.