That is, I have a list of class ‘Entity‘, which is an abstract class containing several different classes.
In the list, I can determine which are of a specific descendant classes using
if (list[i] instanceof Enemy)
However, I cannot, using the list[i] reference, reference fields which are specific to Enemy and not inherited.
Is there any simple way of grabbing a reference to it which will let me reference specific fields?
Otherwise I’ll just rewrite that structure of the code.
Solved: Explicitly casting:
(Enemy)(list[i])
You can cast the element to the appropriate class.
However, when working with a mixed collection of a base type, it is generally because you’re only interested in the polymorphic behaviors, so I encourage you to follow through on at least exploring that particular area of your code. (On that note, it is also generally discouraged to access member fields or variables directly, as your question implies you wish to do, as that violates encapsulation.)