For the following question,
Suppose the object referenced in a cell of
ArrayList<Polygon> pentagonGroup
is of type Pentagon. If you later reference that object, what type will it have?
What would the answer be?
I tried playing around in a IDE and it seems like child objects even when placed into a Parent list are identified as a Child when doing the following operation.
Child c = new Child();
ArrayList<Parent> pa = new ArrayList<Parent>();
pa.add(c);
Class cls = pa.get(0).getClass();
System.out.println(cls.getName());
This seems kind of odd when one is unable to use Child object specific methods without downcasting it back to an Child.
System.out.println(pa.get(0).getChildMessage()); // invalid
System.out.println(((Child)pa.get(0)).getChildMessage()); // valid
Given my results the answer to the first question would be (Pentagon). Would that be correct?
EDIT:
So I'm actually still not sure about the very first question asked above; Would the answer be Polygon or would it be Pentagon?