I am trying to make an ArrayList that can be used by all subclasses of one abstract class, but the object types should be of the sublcasses.
To make it simple, here’s an example:
There is an abstract class called ‘Animal’, and there are three subclasses called ‘Dog’, ‘Cat’, ‘Pig’.
So, I want to make an ‘
ArrayList<Animal> all‘, and this list should be used by the subclasses to store the objects in their lists.
So, there must be an ‘
ArrayList<Cat>‘ and ‘
ArrayList<Pig>‘ and so on.
I don’t want to loop through the ‘Animal’ class, then convert each object cause that will make the non-inherited methods and values inaccessible…
I wanna do this so I can iterate through the objects faster.