Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 683801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:44:51+00:00 2026-05-14T01:44:51+00:00

Given the following situation (UML below), If Y has the method: public void PrintWs();

  • 0

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

Sorry, to clarify:

  • Z contains 1 Y object.
  • Y and Z are both subclasses of P
  • The image seems to work if you refresh – My reputation is too low to upload images, so I will edit when I acquire 15 points 🙂
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:44:51+00:00Added an answer on May 14, 2026 at 1:44 am

    You can’t – assuming you only want to try to call PrintWs on instances of Y, you need to determine which references are pointing at instances of Y… and that’s where you use instanceof. (You could use Y.class.isInstance(p) but that’s just the same thing in a slightly different form.)

    Of course if you can make P contain a no-op PrintWs which is then overridden in Y, then you can call it on everything in the list…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code : import java.io.*; public class Main { public static void
So, given the following code: public MyInterface getMyInterface() { return new MyInterface() { public
Given the following situation, Entity Framework Code-first missnames/renames the connecting or Join table. public
Given the following situation: // Initializing a new composite within a parent UI container
Given the following situation: @Entity @Inheritance(strategy = InheritanceType.JOINED) @Table(name=TOP_TABLE) public class BaseClass implements Serializable
Given the following situation: PC --- |aa RTR1 bb| --- |aa RTR2 bb| ---
Given the following situation: char const a = (i == 0) ? 0 :
Given the following code sample: public class WeirdStuff { public static int doSomething() {
Given the following situation: @try { @try { // raises an exception :) [receiver
There is a following situation: A class is given, with a generic field in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.