Please correct me if I’m wrong (!), but I believe there are a couple of problems with this Check For Understanding.
- In the given LimitedProduct class, and the overridden method decreaseQuantity, there is no direct access to quantity since it is a private instance variable of the super class and should use getQuantity() instead.
- instead of trying to update quantity, the method should call super.decreaseQuantity(amount)
- The answer to the Check For Understanding should be A instead of C
Please advise - thanks!
public class LimitedProduct extends Product {
private int limit;
public LimitedProduct(String name, int quantity, int limit) {
super(name, quantity);
this.limit = limit;
}
public void decreaseQuantity(int amount) {
if (quantity - amount >= limit) {
quantity -= amount;
}
}
}
Hi, thank you for your well organized post, the details helped me to check on what you are asking about
- I agree, there is an issue with the scope of the quantity variable, it is private to the super class. We would need to call the getQuantity() method to access that value.
- what you described would work, if you put the call to the super version of the decreaseQuantity method inside of the if statement’s block.
- answering if they are the same or not is confusing-- the question talks about them having the same limit values, but limit is only an instance variable for the sub class, so a Product object won’t have a limit. If they are created with the same quantity and then decreaseQuantity is called with the same amount, they could end up with the same quantity if the sub class’s limit allows the decrease to happen. Or if the limit restricts the decrease from happening, then my read on this question is that LimitedProduct could have a higher quantity.
Thank you for posting about this issue on the forum. I will pass this info onto support@code.org and let them know this question needs revision. That will take a bit of time, if you are doing this lesson with students soon, you could just tell them to skip this level. Or, you could bring up this level with the class and discuss how to rewrite the class to correct the scope issues.
Best,
Lindsay
1 Like