I’m a beginner in Java programming and would like to understand what is the right way for working with polymorphic references here.
Suppose we have the following (abstract) code in C++:
List<Fruit*> lstFruit;
...
// collect apples here
while (...)
{
Fruit* apple = new Apple("green");
lstFruits.append(apple);
}
...
lstFruit[i]->doSomething(); // here is our virtual method
Java doesn’t let me create a List collection with references to a base class that is abstract. What should I do in this situation? My goal is to work with different objects of child classes universally, no matter what their class actually is.
You can create a generic collection with astract class: