Question

I'm trying to create a superclass that stores detail for both a class InPatient and OutPatient which store common methods. I've managed to move over the id field with no problems but am struggling when it comes to moving over medication. on the setMedication method when changing

this.medication = medication;

to

this.getMedication() = medication;

I am told that I should be inserting a variable instead of a value but cannot figure out what variable I should be putting in it's place instead.

I have the class InPatient:

public class InPatient extends Patient
{   
    // The condition of the patient.
    private String status;
    // Whether the patient is cured or not.
    private boolean cured;

    public InPatient(String base, String id)
    {
        status = base;
        cured = true;
    }

    /**
     * Set the patient's medication
     * The patient is no longer cured
     */
    public void book(String medication)
    {
        setMedication(medication);
        cured = false;
    }

    /**
     * Show the patients details.
     */
    public String getDetails()
    {
        return getID() + " at " + status + " headed for " +
        getMedication();
    }

    /**
     * Patient's status.
     */
    public String getStatus()
    {
        return status;
    }

    /**
     * Set the patient's medication.
     */
    public void setMedication(String medication)
    {
    this.getMedication() = medication;
    }

    /**
     * Show that the patient is cured
     */
    public void better()
    {
        status = medication;
        medication = null;
        cured = true;
    }
}

and it's superclass Patient:

/**
 * Write a description of class Patient here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Patient
{
    // Patient's ID.
    private String id;
    // Medication the patient is on.
    private String medication;

    /**
     * Constructor for objects of class Patient
     */
    public Patient()
    {
        this.id = id;
        medication = null;
    }

    /**
     * The patient's ID.
     */
    public String getID()
    {
        return id;
    }

    /**
     * Patient's medication
     */
    public String getMedication()
    {
        return medication;
    }
}

What is it that I'm missing here?

Was it helpful?

Solution

You can't put a function call on the left-hand side of an assignment. That just doesn't make sense.

If medication weren't private, you could just have your method assign to it. Since it's private, you can't. There are two solutions:

  1. Change private to protected in the Patient class. This lets child classes access it directly, while outside clients still can't see the medication field.

  2. (my preferred method) Add a setMedication method to the parent (Patient). You could make this protected so that child classes could call setMedication while outside clients could not. In any event, the InPatient class would use that method to set medication (using the syntax super.setMedication(medication) so that it calls the method in the parent class).

OTHER TIPS

The base/parent class:

class Patient {

    private String medication;

    public String getMedication() { return this.medication; }
    public String setMedication(String __medication) {
        this.medication = __medication;
    }
}

The child/extended class:

class InPatient extends Patient {

    public String getMedication() { super.getMedication(); }
    public String setMedication(String __medication) {
        super.setMedication(String __medication);
    }
    // if medication is being instantiated using the value from parent class
    public String setMedication() { 
        this.medication = super.getMedication();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top