My previous OOP experience was with Objective-C (which is dynamically typed), however now I am learning Java. I want to iterate over ArrayList objects and execute a specific method on them. Each object in an ArrayList has one class. In Objective-C, I would simply check at each iteration that the object was the correct class, and then run the method, but this technology is not possible in Java:
for (Object apple : apples) {
if (apple.getClass() == Apple.class) {
apple.doSomething();
}
}
How do I say "compiler, to which class do the objects in ArrayList belong?
source
share