My setup is the following. I have a superclass called "bottles" and 2 different subclasses, "water" and "soda". There's an array filled with countless of both of these and my goal is to write a method that returns the total liter values of only one of these subclasses. What would be the best way to accomplish that? As far as i understand 'instanceof' has no use here.
To clarify i only want to know what operator/method is used to compare:
if (bottles[0]==soda){
...
}
IIRC, instanceof still works.
However, how about you put the logic of your counting-liter-criteria in your subclass method? So that, if you want to count the liter of sweet drink in your bottles, your water should return 0 but your soda should return real liter. Since the subclasses know the best of itself, it is better you let them give you the answer directly. Don’t break encapsulation.