We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
//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.
extends
, it already inherits all members, including fields!You can also implement a constructor in Button and call
super
from AsteriskButton, passing those arguments to Button in order to initialize those fields: :-bdYou can read more about
abstract
classes here:https://forum.Processing.org/two/discussion/13616/inheritance-in-a-class-with-an-array-loop
@Chrisir @Ribo ===
It seems to me that GoTOLoop is right vs Chrisir.
@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@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!
The choice of using an
abstract
class was @Ríbó's.And
abstract
classes are supposed to haveabstract
methods! ~O)@goToLoop=== at least one! :-h
Thanks very much everyone!