0

I have a class Status that has 3 methods:

getPedestrianStatusText()
getGatesStatusText()
getDrawBridgePositionText()

(The purpose of each of these methods is to contain a String that is the status of something, and should change when a button is pressed).

and I have a class FileStatus that extends Status and it access those methods as follows:

public class FileStatus extends Status{

public FileStatus(){

    }

public void writeToFile(){
        String file_text = getPedestrianStatusText() + "     " + getGatesStatusText() + "     " + getDrawBridgePositionText();

    }
}

In a separate class these 3 methods are accessed and behave as expected, when a button is pressed and they are called they change as I have them programmed to. My issue is that when those methods are accessed from the FileStatus class the String returned does not change as it does when those methods are called from a different class. I suspect I have an error in my inheritance but I am not sure what the issue is. Any advice is appreciated.

This is my Status class:

public class Status{

public Status(){
}



public String getGatesStatusText(){
    //returns a string with the status of the gates

    if(GatesOpen){

        gatesStatus="The gates are open";
    }
    else{

        gatesStatus="The gates are closed";
    }
    return gatesStatus;
}



public String getDrawBridgePositionText(){
    if(DrawbridgeUp){
        DrawBridgeStatusText="Drawbridge is up";
    }
    else{
        DrawBridgeStatusText="Drawbridge is down";
    }
    return DrawBridgeStatusText;
}

public String getPedestrianStatusText(){

    return pedStatusText;
}

}

This is a seperate class where FileStatus is called:

public class MenuBar extends JMenuBar {
DrawStatus draw;
public MenuBar(DrawStatus d){
    final FileStatus file = new FileStatus();

    });


    JMenuItem printStatus = new JMenuItem("Print Status");
    printStatus.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event){
            file.writeToFile();
        }
    });

} }

Finally the last class where the functions behave as expected:

public class DrawStatus extends JPanel{
Status status = new Status();


public DrawStatus(){

}


@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);

    g.drawString("SYSTEM STATUS: "+status.getGatesStatusText() + " | " +status.getPedestrianStatusText() + " | " + status.warningLightStatusText() + " | "+status.getDrawBridgePositionText(), 0, getHeight()-10);

}
4
  • 1
    I think you will have to share Status class code too... You might want to override those 3 methods depending on your needs.
    – wns349
    Apr 7, 2014 at 0:27
  • Also, you need to share the code that is using the Status and FileStatus instances.
    – cybersam
    Apr 7, 2014 at 0:33
  • A possible cause of the problem is that you're updating the values in DrawStatus.status, and expecting them to be automatically updated in FileStatus too. Are DrawbridgeUp, GatesOpen and pedStatusText members of Status or global variables? If they're members, then you'll have to stop using inheritance and instead compose FileStatus from Status (which is probably a good thing to do anyway)
    – Paul Hicks
    Apr 7, 2014 at 1:16
  • I understand what you are saying about updating the values in DrawStatus.status but I dont understand why those values would not also get inherited in FileStatus , I thought that by extending Status my FileStatus class would inherit the values in Status. GatesOpen, etc are members of Status, declared directly below the declaration of the class Status.
    – ez4nick
    Apr 9, 2014 at 23:27

0

Your Answer

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

Browse other questions tagged or ask your own question.