I have a class PartA
and a class PartB
which is a derived class of PartA
.
I have a list of type PartA
which holds both parts a and b.
And I need to determine which kind of class it actually is when I loop through it.
But if I do this:
for (PartA i : parts) {
if (i instanceof PartA) {
//some logic
}
It will still give true regardless of which class it actually is: a or b.
Am I doing something wrong or if not, what can I do to acheive desired result?
This behavior is by design.
Instance of B
are also instances of A
.
You want
if (i.getClass() == A.class)