Could someone please explain me why am I getting this output
package code;
import java.util.ArrayList;
class A {
}
class B extends A {
}
class C extends B {
}
public class ThreadDemo {
public static void main(String[] args) {
ArrayList<A> x = new ArrayList<A>();
ArrayList a = new ArrayList();
x.add(new A());
a = x;
a.add(new B());
ArrayList b = a;
ArrayList<C> c = (ArrayList<C>) b;
c.add(new C());
a.add(new A()); // I am not sure why object of A is also getting printed at the last
for (Object obj : c) {
System.out.println(obj + " class Name " + obj.getClass().getName());
}
}
}
Output :
----------
code.A@7852e922 class Name code.A
----------
code.B@4e25154f class Name code.B
----------
code.C@70dea4e class Name code.C
----------
code.A@5c647e05 class Name code.A
----------