1

[I know the following does not respect OOP rules, this is an early-dev project. I will do all the setters and getters later]

I have a class called 'Item', which contains a private field 'name'.

abstract class Item{
  protected PImage texture;
  protected int durability;
  protected int maxDurability;

  private String name;

}

I also have a class called 'Armor', which inherit 'Item'. So it should inherit the private field 'name', right ?

class Armor extends Item{
  protected int defense;

  Armor(){
    //First try to change the value
    name = "Armor";
    //Second try using 'this' to be sure it doesn't try to change super.name
    this.name = "Armor";
  }
}

In both cases, I have an error when I try to change the value : "The field Time_Fighter.Item.name is not visible".

After reading some stuff about how 'private' works in Processing, I discovered some people proposed to use 'protected' instead.

The thing is, if I use 'protected', every classes have access to it. But I just want 'Item' and the subclasses of 'Item' to have access to their private fields inherited from 'Item'.

I might have done a pretty obvious error since I'm kinda just a beginner, so if this is not the intended way of doing so, please tell me how I'm supposed to do it...

[Edit. It seems from answers I've seen that this is not possible this way. So here's there any way to have a variable that would only be accessible for subclasses and not all the package ?]

CLOVIS
  • 511
  • 6
  • 17
  • "Right?" Wrong. Private fields are not inheritable. And `protected` means that only subclasses (and members of the same package, see [tutorial](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)) can access it. The one that allows access to all classes is `public`. – RealSkeptic Mar 27 '16 at 18:15
  • @RealSkeptic, except in Processing packages are not allowed so 'protected' does the same as 'public' – CLOVIS Mar 27 '16 at 18:47
  • Here's a good [cheat sheet](http://stackoverflow.com/a/33627846/276052) for access modifiers in java. – aioobe Mar 27 '16 at 19:10
  • Did you ever get this figured out? Did any of the answers help you? (If so, you might consider upvoting and accepting one of them.) – Kevin Workman Mar 31 '16 at 13:31
  • @KevinWorkman done, I'm new here so I forgot... In the end I used a constructor – CLOVIS Apr 10 '16 at 14:20

3 Answers3

1

You can't.

Private variables are just that: private. That means a subclass can't access them.

You might instead add a setter() function that the subclass uses.

Normally you would just make it protected, but Processing doesn't use packages, so it ends up being the same as public.

Honestly, I wouldn't worry too much about this. Processing is designed to make things simple, so it skips over this topic a bit. If you really want this functionality, you might consider writing Java code (which can call Processing code), but that's a lot more involved than just writing Processing directly.

In your case, you might create a constructor in your Item class that takes a name as a parameter. Then you can call that constructor from your Armor class and pass in whatever value you want:

abstract class Item{
  private PImage texture;
  private int durability;
  private int maxDurability;

  private String name;

  public Item(String name){
      this.name = name;
   }
}

class Armor extends Item{
  private int defense;

  Armor(){
    super("Armor");
  }
}
Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
  • So how can I have a variable that only one class and it subclasses have access to ? – CLOVIS Mar 27 '16 at 18:23
  • @CLOVISArtificialIntelligence You can't. The closest you can get is `protected`, but that doesn't make a ton of sense in Processing. It only makes sense in Java, which uses packages. In Processing, nothing is in a package, so `protected` doesn't make as much sense. If I were you I wouldn't worry about this until you start using Java instead of Processing. – Kevin Workman Mar 27 '16 at 20:25
  • @CLOVISArtificialIntelligence I've edited my answer to include the approach I would take if I were you. – Kevin Workman Mar 27 '16 at 20:46
  • So if I understood well the fields ARE inherited, and the subclasses DOES HAVE the private fields somewhere, but can't modify them itself, so if they want to use/modify them the subclass needs to use a function of the superclass ? – CLOVIS Apr 10 '16 at 14:22
  • @CLOVISArtificialIntelligence That sounds about right. The **instance** still has the variable, but the subclass can't access it directly. You have to go through functions that the super class makes available. – Kevin Workman Apr 10 '16 at 16:20
0

You can make your name field protected and place your Item and Armor classes to a separate package. Fields declared as protected are available to the classes of the same package and descendant classes.

See Controlling Access to Members of a Class

Stanislav Karakhanov
  • 1,149
  • 1
  • 9
  • 11
0

you can access all class-global variables even private ones via Reflection How do I read a private field in Java? . I use this a lot and it works pretty fine. So if someone really wants to access the variables he can most of the time.

Community
  • 1
  • 1
picatrix
  • 11
  • 4