# Base class with protected method (I do not know a good topic title for this, sorry)

**URL:** <https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523>\
**Category:** Beginners\
**Created:** [July 31, 2023, 5:32am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523 "2023-07-31T05:32:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [July 31, 2023, 5:32am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/1 "2023-07-31T05:32:20Z")

</div>

Good day,

I have an abstract class from which I derive other classes.

```java
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.

```java
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.

```java

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 😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 31, 2023, 9:17am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/2 "2023-07-31T09:17:19Z")

</div>

> [@sterretje](#):
>
> Is this a flaw in my thinking about the implementation?

tbh I don’t think that’s possible in processing.

Reason is that processing uses a pre-compiler that writes all your classes into one big class and then compiles.

Hence, all the classes become sub-classes

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 31, 2023, 10:10am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/3 "2023-07-31T10:10:53Z")

</div>

> [@sterretje](#):
>
> `myForm.testMouseposition(); // <<<< this here should not be possible`

Everything inside .pde files are concatenated as 1 big PApplet subclass.

Access keywords like `protected` or `private` can’t disallow PApplet methods such as **setup()** or **draw()** from accessing members from inner classes within a “.pde” file.

> [@sterretje](#):
>
> … to prevent an instance of the derived class to access the **testMouseposition()** method…

So you’re gonna need to move its class to a separate “.java” file.

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [July 31, 2023, 11:57am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/4 "2023-07-31T11:57:10Z")

</div>

Thanks people for the replies.

@Chrisir, I have a vague understanding how it works (Arduino does, not quite surprisingly, basically the same when using multiple pde/ino files).

@GoToLoop, I did not know that I could add java files in Processing’s IDE. But a basic test seems to show that I can. Will work that out later.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 31, 2023, 12:41pm UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/5 "2023-07-31T12:41:58Z")

</div>

Some sketches w/ “.pde” + “.java” files:

- [“Countdown Class”](https://gist.github.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21)
- [“Brownian Motion”](https://gist.github.com/GoToLoop/145418d1dfb062c7c41a630bce5b8374)
- [“Hacked Movie EoS Event”](https://gist.github.com/GoToLoop/67023ba3e9ba1d0301c40f7c030d176a)
- [“Keyboard Input Library”](https://gist.github.com/GoToLoop/bba0c288aaeeb5ef9bb1)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 1, 2023, 8:53am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/6 "2023-08-01T08:53:55Z")

</div>

It seems to me that you want all GUI classes to inherit from your GuiControl class but only some of them to be able to see the `testMouseposition` method.

The solution is straightforward

1. Create a new abstract class called `GuiControlMouse` that extends the `GuiControl` class (the actual name is not important)
2. _ **Move** _ the `testMouseposition` method from `GuiControl` to `GuiControlMouse`

The GuiForm class is unchanged but can no longer see the `testMouseposition` method. Any classes that need to use this method should extend the `GuiControlMouse`

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [August 1, 2023, 9:22am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/7 "2023-08-01T09:22:36Z")

</div>

Thanks for the reply. All GUI classes should see the testMousePosition() and use it internally. But the instances of those classes should not be able to see / use it.

I have a GuiForm, a GuiButton and a GuiTextbox class at this moment, all deriving from GuiControl. A form instance can contain instances of the other two (and actually of a GuiForm as well (so nested forms)). Each derived class above has a public onClick() method that calls the testMousePosition() so the user application e.g. can set the focus on a textbox or execute a method associated with a button when clicked. The user application has no need to know about testMousePosition(), only about onClick().

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 1, 2023, 10:27am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/8 "2023-08-01T10:27:38Z")

</div>

I _now_ understand what you you want to do 😁 unfortunately its not possible in Processing.

When you create a basic sketch, Processing stores your code in one or more .pde files (one per tab). When you execute the sketch Processing combines all the pde files into one and creates a Java class for the code, the name of the class being the name of the sketch.

If you create a class inside a pde file it becomes an _ **inner class** _ and all attributes (fields and functions) of inner classes are treated as `public` by the outer class and vice-versa.

Try this code

```auto
private float pi = 3.142;

void setup() {
  Foo f = new Foo();
  println(f.n, f.get_pi());
  exit();
}

class Foo {
  private int n = 42;

  private float get_pi() {
    return pi;
  }
}

```

The output is `42 3.142` proving what I say.

In Processing we can promote our inner class to a top-level class like this

```auto
public static class Foo{
   ...

```

`Foo` can no longer access `pi` but the attribute `n` is still visible but so this hasn’t helped. The other thing to try is putting the `Foo` class in its own tab called `Foo.java` so we have  
**main pde tab**

```auto
void setup() {
  Foo f = new Foo();
  println(f.n);
  exit();
}

```

**Foo.java tab**

```auto
public class Foo {
  private int n = 42;
}

```

Now `println(f.n);` fails because `n` is a private attribute but if we make it public then it works as expected.

So what happens if we make it protected … ?

```auto
public class Foo {
  protected int n = 42;
}

```

You might be surprised to know that `println(f.n);` still works!!!

In C++ `protected` limits access to the class and any child class as you might expect, this is not the full story in Java.

In Java it is called _package protection_ so that `n` is accessible to the Foo class, any class that inherits from Foo and any class in the same _package_. Java packages are based on the storage hierarchy so any Java classes in the same folder/directory are in the same package.

Since Processing stores all the .pde and .java files in the same folder they are all in the same package so protected attributes will be treated as public.

This shows that protected access will not provide the access level you want in the Processing IDE.

Hope I have explained this clearly 😁

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 1, 2023, 11:35am UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/9 "2023-08-01T11:35:59Z")

</div>

> [@quark](#):
>
> Since Processing stores all the .pde and .java files in the same folder they are all in the same package…

The “.java” file created outta the concatenated “.pde” files don’t have a `package` statement.

And a “.java” file w/o a `package` statement belongs to the so-called “default package” as well.

If our own “.java” files don’t have a `package` statement either, they’ll also go to the “default package”.

BtW, all my 4 Gist example links use keyword `package` and therefore those “.java” files require the `import` keyword using their `package` namespace.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 1, 2023, 1:07pm UTC](https://discourse.processing.org/t/base-class-with-protected-method-i-do-not-know-a-good-topic-title-for-this-sorry/42523/10 "2023-08-01T13:07:42Z")

</div>

After reading @GoToLoop I thought

> ‘Is it possible simply to name your own packages in Processing?’

So I decided to revisit the problem and create a sketch that mimics the issue raised by the OP but with packages.I believe that the original problem boils down to

> How do I make protected attributes and methods of a class unavailable to instances of the class in Processing?

I believe this sketch does just that  
**main sketch tab**

```auto
import foopack.*;

void setup() {
  Foo f = new Foo2(); // use polymorphism
  // println(f.p); // fail private field of top-level class
  // println(f.q); // fail protected field of top-level class
  println(f.r);
  println("------------------------------");
  f.printValues();
  // f.printMessage("Hello world"); // fail protected method of top-level class
  exit();
}

```

**Foo.java tab**

```auto
package foopack;

public abstract class Foo {
  private int p = 31;
  protected int q = 42;
  public int r = 99;

  public abstract void printValues();
  protected abstract void printMessage(String mess);
}

```

**Foo2.java tab**

```auto
package foopack;

public class Foo2 extends Foo {

  public void printValues() {
    // System.out.println(p); // fails private member of parent class
    System.out.println(q);
    System.out.println(r);
  }

  protected void printMessage(String mess) {
    System.out.println(mess);
  }
}

```

Notice that protected fields and methods are no longer available by instances of the class which is the desired outcome.
