Hello,
I am trying to write my first ever assignment in Java and am straggling with it.
I have a superclass LivingThing and two subclasses, Monster and Human. Both subclasses have an attribute 'strength', but the maximum value of it is different for each of them. In my superclass I wrote a method setStrength(int strength), which checks if strength is within minStrength and maxStrength. How could I define different maxStrength values in subclasses and push this value up to the superclass while creating a new object?

Thanks
Anna

Recommended Answers

All 6 Replies

Hello,
I am trying to write my first ever assignment in Java and am straggling with it.
I have a superclass LivingThing and two subclasses, Monster and Human. Both subclasses have an attribute 'strength', but the maximum value of it is different for each of them. In my superclass I wrote a method setStrength(int strength), which checks if strength is within minStrength and maxStrength. How could I define different maxStrength values in subclasses and push this value up to the superclass while creating a new object?

Thanks
Anna

In your constructor you pass your min/max strenght as follow:

super(minStrenght, maxStrenght);

Works like this :) Thanks!

It does work, but what happens when you need to set other values that are specific to the subclasses? Then you have to add more parameters to the constructor and alter all the calls to it.

A more flexible way to go about it is to make the maxStrength variable "protected" in the base class, which will allow subclasses to access it, or to create a protected method setMaxStrength() that can be used by child classes.

It does work, but what happens when you need to set other values that are specific to the subclasses? Then you have to add more parameters to the constructor and alter all the calls to it.

A more flexible way to go about it is to make the maxStrength variable "protected" in the base class, which will allow subclasses to access it, or to create a protected method setMaxStrength() that can be used by child classes.

I've tried both these methods before, but they set maxStrength too late. In my subclass I call superclass constructor and one of its parameters is strength, which then is checked in setStrength() and assigned to this.strength. setStrength() uses maxStrength value, so maxStrength has to be assigned before it happens and I cannot call setMaxStrength() from subclass before calling superclass constructor.

Hope it makes sense.
Anna

Ok, I didn't realize that was part of the base constructor as well. So now your base constructor takes minStrength, maxStrength, and strength? I'm assuming it still needs the strength value as before, otherwise it's not really initializing the data. That would seem to leave you in a worse position than before, design-wise. Now you need to pass 3 parameters for every attribute that you wish to have on the object. If your LivingThing has 8 attributes, then you will be passing 24 arguments to your constructor, which is just painful and unnecessary.

Personally, I would still recommend going with protected methods to set that state in the base class and pulling those values out of the base constructor, but that's just me.

So the less parameters the better? I thought that it was better to send the arguments which are the same for all subclasses up to their superclass.

Could you also advice me on menu class? I have one class with menu() method and other methods for each of the menu options, like addHuman(), addMonster(), deleteEntity(). Is this alright or should I divide it into 2 classes? I just don't know what is a good code practice in Java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.