On private keyword

Hello,
maybe I’m getting confused but I was expecting for this print line to return an error since I’m trying to access a private field (age) from outside its class (Parent)? Thanks


class Parent{
  private int age;
  
  public Parent(int age){
    this.age = age;
  }
}

class Child extends Parent{
  private String name;
  
  public Child(String name, int age){
    super(age);
    this.name = name;
  }
}

void setup(){
  Child c = new Child("Tom", 4);
  Parent p = new Parent(40);
  println("C: " + c.name + " - " + p.age); //I was expecting p.age to not work
}
1 Like

Processing.org/reference/private.html

  • All members inside “.pde” files belong to (are nested inside) a single PApplet subclass! :open_mouth:
  • Nothing is actually private among them. :male_detective:
  • However, access restriction would still apply when an access attempt is made from within a “.java” file to a “.pde” 1; and vice-versa. :no_entry:
1 Like

I see, thanks! I think the reference page is misleading since it says " In Processing, all fields and methods are public unless otherwise specified by the private keyword.".

Processing’s IDE (PDE) has a preprocessor that, among many other things, prefixes all methods w/ the keyword public if they don’t have 1 already on “.pde” files. :flushed:

1 Like