Help is needed. The super-class method from the child is not called.

About code:

This code is a few warehouses that accept some products, and when the NoIceboxWarehouse full (9 stores as much as possible), then all the products go to NoIceboxReserveWarehouse . It’s not worth paying attention to the Warehouse itself since it was created when the conditions that the warehouse might overflow did not exist. And here I just have to add such an extension so that it becomes possible, but so that the old tests do not break.

I have several classes in the inheritance chain. I am trying to call the parent method from the child:

Parental:

 public class Warehouse implements Repo { /** * List contain products. */ protected ArrayList<Product> products = new ArrayList<>(); @Override public ArrayList<Product> getProducts() { return this.products; } @Override public String getName() { return "warehouse"; } @Override public boolean isAppropriate(Product product) { return (product.checkQuality() < 26); } } 

Here is his heir:

 public class NoIceboxWarehouse extends Warehouse { /** * This repo contain only 9 products, all next after 9, go in NoIceboxReserveWarehouse. * @return "" if filling equal 10 products. */ @Override public boolean isAppropriate(Product product) { return this.products.size() < 9 && (product.checkQuality() < 26) && this.checkIceFlag(product); } private boolean checkIceFlag(Product product) { return !product.getFlag().equals("ice") && !product.getFlag().equals("canReproduce"); } } 

And another level:

 public class NoIceboxReserveWarehouse extends NoIceboxWarehouse { @Override public String getName() { return "NoIceboxReserveWarehouse"; } /** * Check base repo reached the limit. * @param product product for check. * @return result base repo limit and product quality. */ @Override public boolean isAppropriate(Product product) { // вот тут проблема: // >>return super.getProducts.size() > 8<< ... return ControlQuality.getNoIceboxWarehouse().getProducts() .size() > 8 && (product.checkQuality() < 26); } } 

In the last class in the isAppropriate(Product product) method isAppropriate(Product product) I want to check the status of the parent NoIceboxWarehouse object NoIceboxWarehouse such a record (I have it commented in the code): super.getProduct.size() > 8 but all the time I get zeroes have to go to the class that stores these repositories create static pointers and pull state on them:

 ControlQuality.getNoIceboxWarehouse().getProducts().size() > 8 

I want to do something more sophisticated but something doesn't work, why is super not working? Is it because he doesn't know what specific copy I mean?

Can I somehow get access to the class field without referring to the place where the object of this class is created, through inheritance or something else? Here help please advice that I do not understand?

  • one
    super always "knows" which implementation up the hierarchy should be addressed - there is no diamond-shaped inheritance in java (which you are hinting at, as I understand it), so this problem is irrelevant. The implementation is in the parent class of the parent, Warehouse, so a method will be called from there. It is not clear what you think means "does not work." - AseN
  • It seems to me that you are super not needed here at all and you can simply call getProduct.size (). - Yuriy SPb
  • >>> It is not clear what you think means "does not work" <<< I meant that I want to call through the super method of the parent class that was defined above by its parent. - Pavel
  • one
    @Pavel, this method is called exactly - there is simply nothing more to be called (in the virtual functions table, the pointer to the initially redefined one did not change in the parent gap). Therefore, it is still not clear what is not working for you (what kind of error)? - AseN
  • Well, yes! The fact is that there is another ControlQuality class that stores NoIceboxReserveWarehouse and NoIceboxWarehouse objects and I need to monitor their states as there are already added products. And when I call ControlQuality.getNoIceboxWarehouse (). GetProducts (). Size () shows me how many products are correct and when I call super.getProducts (), it always returns 0. - Pavel

1 answer 1

From what I see. In the Warehouse class there is a getProducts() method and the getProduct() method is getProduct() . Either you have a typo in the question, or there are two methods getProducts() and getProducts()

Further, in the NoIceboxReserveWarehouse class NoIceboxReserveWarehouse the getProduct() method is not overridden, so super is not needed at all.

Finally, check in the method NoIceboxReserveWarehouse.isAppropriate() that

 ControlQuality.getNoIceboxWarehouse() == this 

chances are that you are working with different objects

  • getProduct () yes this is a typo in question - Pavel
  • one
    @Pavel Another assumption - Anton Shchyrov
  • Yes, in general, I have already checked that ControlQuality.getNoIceboxWarehouse () == this - false, I'm just trying to figure out why. - Pavel