A Java class whose fields are available only for its subclasses (without getters / setters)?

What I really want is a class with a universal constructor, and when the identical constructor of the subclass is called, the subclass will have access to the same fields. Here is an example of what I would like to do:

public abstract class Command{ private Mediator m public Command(Mediator med){ m = med; } abstract void exec(); } public class FoobarCommand extends Command{ public FoobarCommand(Mediator med){ super(med); } public void exec(){ med.doAFoobar() } } public static void main(String[] args){ Mediator m = new Mediator(); Command c = new FoobarCommand(m); c.exec(); } 

Obviously, this will not work because FoobarCommand does not have direct access to Mediator med. So, how would you get access to the medial area? I don’t want anyone other than subclasses to have access to it, and “protected” is not an option, because I want people to be able to create their own teams (which, obviously, would be outside the package).

+4
source share
7 answers

Actually there is no such access modifier, strictly speaking. It is not possible to declare a field (or method / class, for that matter) accessible only to subclasses; the most restrictive modifier you can use is protected , which still allows you to access other classes in the package of the parent class.

But besides this nigga, protected is the way to go.

Edit: to clarify that protected is an option. To access a protected method, you must be either a subclass or inside the same package; you don’t have to be both. This way, a subclass of Command created in another package can still access (super).m .

+12
source

Declare Mediator Med as “protected,” not closed.

+5
source

you need to declare an intermediary m protected in your mom class.

In addition, in the exec () method in your subclass, you need to do m.doAFoobar () instead of med.doAFoobar (), since med is not a member, but a formal parameter of your constructor.

+2
source
 abstract class Command { protected Mediator m public Command(Mediator med){ m = med; } abstract void exec(); } 

A class is not publicly available, therefore it can be extended only by other classes in one package, and "m" is protected, therefore derived classes can access them.

+1
source

If you provide access with access protection, it will be available for subclasses outside the package.

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

+1
source

If you want the field to be accessible only to the class itself and any derived classes and other classes in this package, use the protected keyword. This is what it is for, and it should work even outside the package. Instead of calling them med.doFoobar () ;, they should call m.doFoobar ();

Alternatively, you can create a secure (or public, even) get function. This way you will find the possibility of obtaining an intermediary, but you do not need to rewrite them after the announcement.

However, what you want (cannot be read inside the package) is not possible in java keywording. But, since you are the one who writes this particular package, could you just not get access to it from within the package? Or create your own package with just this file? It is not possible to allow access to subclasses and not allow classes in the package.

+1
source

What you can do is give the subclass a variable that is called exactly the same, and then set it equal to the superclass using constructors and methods like

 public abstract class Command{ private Mediator m public Command(Mediator med){ m = med; } abstract void exec(); } public class FoobarCommand extends Command{ private Mediator m; public FoobarCommand(Mediator med){ super(med); m = med; } public void exec(){ m.doAFoobar() } } public static void main(String[] args){ Mediator m = new Mediator(); Command c = new FoobarCommand(m); c.exec(); } 

However, this is limited to what he can do. Since m is an object reference, changes to m in the subclass will be reflected in the superclass; however, this would not have happened if the class member were primitive. (Given that all primitives have an equivalent object, this can be worked out if a little clumsy)

The subclass should also receive the link directly, since where the copy of the link is stored. For abstract superclasses, this is fine, since you are guaranteed a subclass, but below you have to be careful how you process the data.

0
source

Source: https://habr.com/ru/post/917195/


All Articles