Good day,
I have an abstract class from which I derive other classes.
abstract class GuiControl
{
GuiControl(int x, int y, int w, int h, String text)
{
...
...
}
...
...
/*
Test if mouse is over control
Returns:
true if it is, else false
*/
protected boolean testMouseposition()
{
// return value
boolean rb = false;
// if the mouse is on the control
if (mouseX > this.ctrlX && mouseX < this.ctrlX + this.ctrlW &&
mouseY > this.ctrlY && mouseY < this.ctrlY + this.ctrlH)
{
rb = true;
}
return rb;
}
}
In a derived class I can access the above method.
class GuiForm extends GuiControl
{
boolean hasFocus false;
GuiForm(int x, int y, int w, int h, String text, PFont font)
{
super(x, y, w, h, text);
this.font = font;
}
...
...
public boolean onClick()
{
hasFocus = testMousePosition();
return hasFocus;
}
}
I would like to prevent an instance of the derived class to access the testMouseposition()
method as there is no need for it.
GuiForm myForm;
void setup()
{
myForm = new GuiForm(...);
myForm.testMouseposition(); // <<<< this here should not be possible
}
void draw()
{
...
...
}
Can it be done? If so, what to read up about. I canāt use private
instead of protected
as the derived class cant see it. I can (obviously) move testMouseposition()
to the derived classes but that means that I have to implement the exact same method in all derived classes which is extra work and, in my view, defeats the purpose of the whole OOP approach.
Is this a flaw in my thinking about the implementation?
Thanks in advance.
Note:
Please no comments on the fact that Iām re-inventing the wheel bui writing some GUI code; this started as a learning exercise. I had a quick look at controlP5 and instead of spending time on understanding it I decided to rather spend time on learning Processing / Java