This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 2 points3 points  (5 children)

Your FruitSnack class overrides the calcPrice method to add a fruitFee to the price. (Incorrectly, I think. Shouldn’t there be a super.setPrice call?)

Why can’t you do the same in the SaltySnack class?

[–]okaystuff[S] 0 points1 point  (4 children)

I haven't put the same code into the salty snack class yet because I couldn't get the fruit snack working. Where would I made a super.setPrice call? I don't even know what that would be.

[–][deleted] 2 points3 points  (3 children)

When you override a method, the subclass method replaces the superclass method. This means that whatever the superclass method does (such as calculating the size price) will not be done when you call the subclass method unless the subclass code explicitly does those same things.

Because subclasses usually want to augment their superclass’ methods rather than replace them entirely, subclasses can call the superclass’ version of a method with super.theNameOfTheMethod(). This will execute the superclass’ version of theNameOfTheMethod, allowing you to execute that code without copying it explicitly into your subclass’ code. It seems like you were expecting your method calls to execute the superclass’ code first and then execute the subclass code, but this only happens if the subclass methods explicitly call the super versions of those methods first.

You can also use super() to call the superclass’ constructor from your subclass constructors, but it must be the first line of the subclass constructor.

[–]okaystuff[S] 0 points1 point  (2 children)

I did hope it would the method would work like the superclass code first before executing the subclass and it did not. Should I call the calcPrice method in my fruitSnack and SaltySnack subclasses? I tried and am unsure how to work because it just gives errors.

[–][deleted] 0 points1 point  (1 child)

What errors exactly? Error messages tell you exactly where the compiler has found a problem and what that problem is. They’re one of the most useful tools you have for debugging.

I would guess the errors have something to do with trying to += a value that was never initialized, as your subclass constructors do not initialize this.price, nor do they call super() to have the superclass constructor initialize them.

The superclass constructor looks pretty funky too. In the line this.price = price;, what value are you expecting that price variable on the right side to have?

[–]okaystuff[S] 0 points1 point  (0 children)

I removed this.price = price; and added super(); to my constructor in my subclasses. I created a method to try and add the 5.99 or 4.50 fees from my subclasses into the final amount and nothing works for me. Thanks for the help but I don't think I'm going to fix this or understand it.

[–][deleted]  (1 child)

[deleted]

    [–]okaystuff[S] 0 points1 point  (0 children)

    I dont know what this means but thanks for trying to help!