Calling Subclass Method in Java

Posted by destructo_gold on Stack Overflow See other posts from Stack Overflow or by destructo_gold
Published on 2010-03-26T09:18:41Z Indexed on 2010/03/26 9:23 UTC
Read the original article Hit count: 296

Filed under:
|
|

Given the following situation (UML below),

If Y has the method:

public void PrintWs();

and X has:

ArrayList <P> myPs = new ArrayList();

Y y = new Y();
Z z = new Z();
myPs.add(y);
myPs.add(z);

How do I loop through each myPs object and call all Ys PrintWs (without using instanceof)?

http://starbucks.mirror.waffleimages.com/files/68/68c26b815e913acd00307bf27bde534c0f1f8bfb.jpg

© Stack Overflow or respective owner

Related posts about polymorphism

Related posts about inheritance