How to use abstract classes in Processing

edited January 2016 in Questions about Code

Hi all.

I'd like to make an abstract class Button and have different kinds of buttons extend it.

So far:

abstract class Button {
  int btnX;
  int btnY; 
  boolean visible;
}

And:

//int  btnX = (image(GUIImages[1]).width/2);//the left side of the button
//int  btnY = (image(GUIImages[1]).height/2);//the left side of the button

//AstriskButton is like a clickable asterisk*. When there is additional info 
//the AstriskButton appears and on_clic() displays the relevant data.

class AsteriskButton extends Button { 
  visible = true;  
  btnX = 200; 
  btnY = 150;
  int  Footnote = 0; //keeps track of which data_page should be linked to when AsteriskButton is clicked.

  void showAsterisk() {
    if (visible == true) {
      image(GUIImages[1], btnX, btnY); //temp button for testing switch
    }
  }




//other code here removed because I know it has errors but they are different errors.

So, when I press the save button, I get a warning in the AstriskButton class editor: unexpected token: visible. I take that to mean Processing doesn't recognise visible as a boolean variable defined in abstract class Button.

How can I fix this? Am I misunderstanding how to use abstract classes?

Thanks.

Answers

  • Answer ✓
    void setup() {
    }
    
    abstract class Button {
      int btnX;
      int btnY; 
      boolean visible;
    }
    
    //int  btnX = (image(GUIImages[1]).width/2);//the left side of the button
    //int  btnY = (image(GUIImages[1]).height/2);//the left side of the button
    
    //AstriskButton is like a clickable asterisk*. When there is additional info 
    //the AstriskButton appears and on_clic() displays the relevant data.
    
    class AsteriskButton extends Button { 
      boolean visible = true;  
      int btnX = 200; 
      int btnY = 150;
      int  Footnote = 0; //keeps track of which data_page should be linked to when AsteriskButton is clicked.
    
      void showAsterisk() {
        if (visible == true) {
          image(GUIImages[1], btnX, btnY); //temp button for testing switch
        }
      }
    }
    
    
    
    
    //other code here removed because I know it has errors 
    //but they are different errors.
    
  • edited January 2016

    //many edits...

    That did it, thank you. But can't I declare a variable in a superclass and assign it's value in subclass?

    Above looks like the AsteriskButton variables shadow the Button variables.

  • edited January 2016

    Above looks like the AsteriskButton variables shadow the Button variables.

    • You're absolutely correct. His solution is re-declaring all Button's 3 fields!
    • When a class uses extends, it already inherits all members, including fields!
    • Simply remove all those field declarations, leaving only footnote, which is exclusive to AsteriskButton.
    • Instead, you can place their initialization inside AsteriskButton's constructor or...
    • Place them inside some initialization block like this: *-:)

    abstract class Button {
      int btnX;
      int btnY;
      boolean visible;
    }
    
    class AsteriskButton extends Button { 
      int footnote;
    
      {
        btnX = 200; 
        btnY = 150;
        visible = true;
      }
    }
    
  • You can also implement a constructor in Button and call super from AsteriskButton, passing those arguments to Button in order to initialize those fields: :-bd

    abstract class Button {
      int btnX;
      int btnY;
      boolean visible;
    
      Button(int x, int y, boolean b) {
        btnX = x; 
        btnY = y;
        visible = b;
      }
    }
    
    class AsteriskButton extends Button { 
      int footnote;
    
      AsteriskButton() {
        super(200, 150, true);
      }
    }
    

    You can read more about abstract classes here:
    https://forum.Processing.org/two/discussion/13616/inheritance-in-a-class-with-an-array-loop

  • edited January 2016
    @GoToLoop, 
    

    @Chrisir @Ribo ===

    It seems to me that GoTOLoop is right vs Chrisir.

    Why not using super()???
    
    with some code like this one:::
    
    
                    void setup(){
    
                      size(500,500);
                      background (255);
                      boolean visi = true;
    
                     AsteriskButton aB = new AsteriskButton(visi, 100,200);
                      aB. showAsterisk();
    
                    };
    
                    //
                    public abstract class Button {
    
                      int btnX;
                      int btnY; 
                      boolean visible;
    
                      public Button( boolean visible,int btnX, int btnY){
                        this.btnX = btnX;
                        this.visible = visible;
                        this.btnY = btnY;
                      }
    
                     void showAsterisk(boolean v, int nX, int nY){
    
                     }
    
                    };
    
                    class AsteriskButton extends Button { 
    
    
                      int  Footnote = 0;
                      //keeps track of which data_page should be linked to when AsteriskButton is clicked.
                      public AsteriskButton(boolean visible,  int btnX, int btnY){
    
                           super(visible,btnX, btnY);
    
                        }
    
                       void showAsterisk() {
    
                        if (visible == true) {
                          //image(GUIImages[1], btnX, btnY); //temp button for testing switch
    
                          fill(0);
                          text("visible", btnX, btnY);
                        }
                      }
                    };
    
  • edited January 2016

    @akenaton, rather than having an empty showAsterisk() method in Button, we can have it as abstract, in order to force any class inheriting it to implement it as well.

    You can read more about abstract classes here: https://forum.Processing.org/two/discussion/13616/inheritance-in-a-class-with-an-array-loop

    // forum.Processing.org/two/discussion/14455/
    // how-to-use-abstract-classes-in-processing
    
    // 2016-Jan-14
    
    abstract class Button {
      int btnX;
      int btnY;
      boolean visible;
    
      Button(int x, int y, boolean b) {
        btnX = x; 
        btnY = y;
        visible = b;
      }
    
      abstract void showAsterisk();
    }
    
    class AsteriskButton extends Button { 
      int footnote;
    
      AsteriskButton() {
        super(200, 150, true);
      }
    
      @ Override void showAsterisk() {
        if (visible) {
          fill(0);
          text("Visible", btnX, btnY);
        }
      }
    }
    
  • @GoToLoop===

    yes; at the beginning i have declared abstract showAsterisk....then i thought that it was adding a new complexity and changed it for a simple void method... thanks!

  • edited January 2016

    The choice of using an abstract class was @Ríbó's.
    And abstract classes are supposed to have abstract methods! ~O)

  • @goToLoop=== at least one! :-h

  • Thanks very much everyone!

Sign In or Register to comment.