Having these classes:
public abstract class Furniture public class Chair : Furniture public class Table : Furniture public class Kitchen { ArrayList <Furniture> furnitures;
I'm looking for best practice that forces me to manipulate a Superclass furniture object as a subclass (Chair or Table).
I tried with a simple application, but when I call a function, it works with a Furniture object, not with a Table or Chair.
I tried something like:
for each Furniture in Array List if(furniture.get() istance of Table) { currentFurniture = (Table) furniture.get(); } else if (furniture.get() istanceof Chair) { currentFurniture = (Chair) furniture.get(); } doSomethingInKitchen(currentFurniture)
I do not know, the problem is that currentMurniture is declared as
Furniture currentFurniture;
And therefore, he will not be recognized by the chairman or the table, despite the casting, or if the design of the solution itself is incorrect.
source share