0
private ArrayList<Doctor> doctors = new ArrayList<Doctor>();

doctors.add(new Doctor());
        doctors.add(new Doctor());
        doctors.add(new Doctor());

        doctors.add(new Surgeon());
        doctors.add(new Surgeon());
        doctors.add(new Surgeon());

for (Doctor doctor: doctors) {
            if (doctor.getAssignedPatient() != null) {
                if (doctor.aDayPasses()) {
                    System.out.println("A " + convertSpecialism(doctor.getSpecialism()) + " treated their patient.");
                    shortBreak();
                }
            } else {
                break;
            }
        }

Works fine however when I try to do this:

        for (Surgeon doctor: doctors) {
            if (doctor.getAssignedPatient() != null) {
                if (doctor.aDayPasses()) {
                    System.out.println("A " + convertSpecialism(doctor.getSpecialism()) + " treated their patient.");
                    shortBreak();
                }
            } else {
                break;
            }
        }

There is a syntax error, how do I for loop through the surgeons I have added to the ArrayList of type doctor.

Assume that Surgeon extends doctor.

16
  • No it's the surgeon that extends doctor: public class Surgeon extends Doctor Commented Dec 7, 2014 at 19:36
  • Tell us the exact error and the row where you get it. Commented Dec 7, 2014 at 19:37
  • Type mismatch: cannot convert from element type Doctor to Surgeon, at for (Surgeon doctor: doctors) { Commented Dec 7, 2014 at 19:38
  • 1
    Yes, user2177940, you can't do what you're trying to do in the second snippet. What's wrong with the code in the first snippet? Commented Dec 7, 2014 at 19:39
  • Ah, misread the problem statement, I thought the last code was the one that worked. Commented Dec 7, 2014 at 19:39

3 Answers 3

2

In two words: you can't. Because ArrayList contains Doctor, and you can't iterate that list as a list of Surgeons as Java doesn't support implicit downcasting. It is the same as assigning Doctor to Surgeon without explicitly downcasting.

So if you want to get Surgeon, you should explicitly convert it to Surgeon like this:

   for(Doctor d :doctors){
        if (d instanceof Surgeon){
            Surgeon s = (Surgeon) d;
            ...
        }
   } 

But that's very bad practice and you shouldn't be doing it.

Sign up to request clarification or add additional context in comments.

1 Comment

This won't make any difference to the result of any of the method calls that look like doctor.something().
0

You can't create a for-each loop (for (SubClass subClass : superClasses)) because the compiler cannot ensure that there are only objects of type SubClass (Surgeon) in there.

Comments

0

You can do like this:

for (Doctor doctor : doctors) {
    if (doctor instanceof Surgeon) {
        if (doctor.getAssignedPatient() != null) {
            if (doctor.aDayPasses()) {
                System.out.println("A "
                    + convertSpecialism(doctor.getSpecialism())
                    + " treated their patient.");
                shortBreak();
            }
        } else {
            break;
        }
    }
}

2 Comments

Is there no better way to do this rather than using instanceof?
No, you don't need to do this at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.