Looks like homework to me, but I'm bored and Java makes me nostalgic.
List list = new ArrayList();
list.add(new A());
list.add(new A());
list.add(new B());
public void printAll() {
for(A i : list) {
System.out.println(i.print());
}
}
class A {
public String print() {
return "A";
}
}
class B extends A {
@Override
public String print() {
return"B";
}
}
The output would look like:
A
A
B
The polymorphic part is when different code is executed for the same method call. The loop does the same thing everytime, but different instance methods may actually be getting called.