Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Here is a class which I have in my program.
- public class NormalStaticPrimaryStatContainer implements IAmPrimaryStatContainer{
- private StaticPrimaryStat strength;
- private StaticPrimaryStat endurance;
- private StaticPrimaryStat finesse;
- private StaticPrimaryStat fortitude;
- private StaticPrimaryStat agility;
- private NormalStaticPrimaryStatContainer(StaticPrimaryStat strength, StaticPrimaryStat endurance,
- StaticPrimaryStat finesse, StaticPrimaryStat fortitude,
- StaticPrimaryStat agility) {
- this.strength = strength;
- this.endurance = endurance;
- this.finesse = finesse;
- this.fortitude = fortitude;
- this.agility = agility;
- }
- private NormalStaticPrimaryStatContainer() {
- strength = new StaticPrimaryStat (1, STRENGTH);
- endurance = new StaticPrimaryStat (1, ENDURANCE);
- finesse = new StaticPrimaryStat (1, FINESSE);
- fortitude = new StaticPrimaryStat (1, FORTITUDE);
- agility = new StaticPrimaryStat (1, AGILITY);
- }
- @Override
- public int getStatValue(PrimaryStatNames stat) {
- switch(stat) {
- case STRENGTH:
- return strength.getStatValue();
- case ENDURANCE:
- return endurance.getStatValue();
- case FINESSE:
- return finesse.getStatValue();
- case FORTITUDE:
- return fortitude.getStatValue();
- default:
- return agility.getStatValue();
- }}
- //There is other stuff here but I will not show it because it is irrelevant.
- }
- //So far, so good. This class also has a subclass, titled NormalGrowingPrimaryStatContainer. Here is the (relevant) code for that class.
- public class NormalGrowingPrimaryStatContainer extends NormalStaticPrimaryStatContainer
- implements IAmGrowingPrimaryStatContainer {
- GrowingPrimaryStat strength;
- GrowingPrimaryStat endurance;
- GrowingPrimaryStat finesse;
- GrowingPrimaryStat fortitude;
- GrowingPrimaryStat agility;
- private NormalGrowingPrimaryStatContainer(GrowingPrimaryStat strength, GrowingPrimaryStat endurance,
- GrowingPrimaryStat finesse, GrowingPrimaryStat fortitude,
- GrowingPrimaryStat agility) {
- this.strength = strength;
- this.endurance = endurance;
- this.finesse = finesse;
- this.fortitude = fortitude;
- this.agility = agility;
- }
- private NormalGrowingPrimaryStatContainer() {
- strength = new GrowingPrimaryStat(1, 1, STRENGTH);
- endurance = new GrowingPrimaryStat(1, 1, ENDURANCE);
- finesse = new GrowingPrimaryStat(1, 1, FINESSE);
- fortitude = new GrowingPrimaryStat(1, 1, FORTITUDE);
- agility = new GrowingPrimaryStat(1, 1, AGILITY);
- }
- @Override
- public int getStatValue(PrimaryStatNames stat) {
- switch (stat) {
- case STRENGTH:
- return strength.getStatValue();
- case ENDURANCE:
- return endurance.getStatValue();
- case FINESSE:
- return finesse.getStatValue();
- case FORTITUDE:
- return fortitude.getStatValue();
- default:
- return agility.getStatValue();
- }}
- public void updateStatValue(PrimaryStatNames stat) {
- switch (stat) {
- case STRENGTH:
- strength.increaseStatValue();
- case ENDURANCE:
- endurance.increaseStatValue();
- case FINESSE:
- finesse.increaseStatValue();
- case FORTITUDE:
- fortitude.increaseStatValue();
- default:
- agility.increaseStatValue();
- }
- }
- //Again, there's more code here which I removed, because it is irrelevant.
- }
- /**You may notice that this is a little... weird-looking. I have a set of identically-named variables, which in theory I shouldn't need at all, plus I have some methods which are nearly identical between them! This is exactly what inheritance is supposed to prevent from happening, so why do I need to do this, you may wonder?*/
- //Well, what I would want to have looks something more like this:
- public class NormalGrowingPrimaryStatContainer extends NormalStaticPrimaryStatContainer
- implements IAmGrowingPrimaryStatContainer {
- private NormalGrowingPrimaryStatContainer(GrowingPrimaryStat strength, GrowingPrimaryStat endurance,
- GrowingPrimaryStat finesse, GrowingPrimaryStat fortitude,
- GrowingPrimaryStat agility) {
- super(strength, endurance, finesse, fortitude, agility);
- }
- private NormalGrowingPrimaryStatContainer() {
- super(new GrowingPrimaryStat(1, 1, STRENGTH),
- new GrowingPrimaryStat(1, 1, ENDURANCE),
- new GrowingPrimaryStat(1, 1, FINESSE),
- new GrowingPrimaryStat(1, 1, FORTITUDE),
- new GrowingPrimaryStat(1, 1, AGILITY));
- }
- public void updateStatValue(PrimaryStatNames stat) {
- switch (stat) {
- case STRENGTH:
- super.strength.increaseStatValue();
- case ENDURANCE:
- super.endurance.increaseStatValue();
- case FINESSE:
- super.finesse.increaseStatValue();
- case FORTITUDE:
- super.fortitude.increaseStatValue();
- default:
- super.agility.increaseStatValue();
- }
- }
- //No code removal this time!
- }
- /** That's a much nicer-looking class, I think, because it only adds the new code that it needs. Everything that the superclass already can do, the subclass automatically inherits, just like it's supposed to! The problem, however, is with that final method, updateStatValue(). The issue is that StaticPrimaryStat objects do not have that method, but GrowingPrimaryStat objects do. This, again, is a case of inheritance, where GrowingPrimaryStat is a subclass of StaticPrimaryStat. The issue, however, is that when that method is called, I get the following error:
- Error:(713, 35) java: cannot find symbol
- symbol: method increaseStatValue()
- location: variable strength of type sample.Main.StaticPrimaryStat
- Now, the exact cause of this error was something I took an embarrassingly long time to figure out, but once I did, I found that it's actually quite simple. Because strength, endurance, finesse, fortitude, and agility are all of the class StaticPrimaryStat, even though I passed it the subclass GrowingPrimaryStat, Java can only recognize them as StaticPrimaryStat objects. Thus, when I call increaseStatValue() on them, an error is produced - after all, StaticPrimaryStat objects do not have any such method. As a result, I can't call this method without redefining each of the five stats as GrowingPrimaryStat objects, and having to rewrite much of the code followed.
- My ultimate question is: How do I resolve this problem? I need to get Java to recognize that I am calling a subclass of StaticPrimaryStat, so that my program can recognize the method in question. Otherwise I violate the DRY principle of not repeating code, and the object inheritance becomes more or less worthless.
Add Comment
Please, Sign In to add comment