[ACCEPTED]-Java class whose fields are only accessible to its subclasses (without getters/setters)?-java
There is actually no such access modifier, strictly 12 speaking. It's impossible to declare a 11 field (or method/class, for that matter) only accessible 10 to subclasses; the most restrictive modifier 9 you can use is protected
, which still allows access 8 to other classes in the parent class' package.
But 7 other than that niggle, protected
is the way to go.
Edit: to 6 clarify that protected is an option. To access 5 a protected method, you must be either a subclass 4 or within the same package; you don't have 3 to be both. So a subclass of Command
created in 2 a different package would still be able 1 to access (super).m
.
Declare Mediator Med as "protected" not 1 private.
you need to declare Mediator m protected 4 in your mother class.
Moreover, in the exec() method 3 in your subclass , you need to do m.doAFoobar() instead 2 of med.doAFoobar() since med is not a member 1 but a formal parameter of your constructor.
abstract class Command {
protected Mediator m
public Command(Mediator med){
m = med;
}
abstract void exec();
}
The class is not public, so it can only 3 be extended by other classes in the same 2 package and 'm' is protected so it can be 1 accessed by derived classes.
If you give med protected access it will 2 be available to subclasses outside of the 1 package.
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
If you want a field to be accessible to 17 only the class itself and any derived classes 16 and other classes in that package, use the 15 protected keyword. That's what it's there 14 for, and it should still work even outside 13 the package. Instead of having them call 12 med.doFoobar();, they need to call m.doFoobar();
Alternately, you 11 could create a protected (or public, even) get 10 function. That way you expose the ability 9 to get the mediator, but you don't have 8 to let them overwrite it after it's declared.
However, what 7 you want (unable to be read inside the package) is 6 impossible in java keywording. But, since 5 you're the one writing this particular package, couldn't 4 you just not access it from inside the package? Or 3 create your own package with just this file? There's 2 no way to allow subclasses access and not 1 allow classes in the package.
What you can do is give the subclass a variable 15 that is named the exact same thing, then 14 set it equal to the superclass using constructors 13 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 12 in what it can do. As m
is an object reference, changes 11 to m
in the subclass will be reflected in 10 the superclass; however, this would not 9 occur if the class member was a primitive. (Given 8 all primitives have an object equivalent, this 7 can be worked around if slightly clunky)
The 6 subclass must also directly receive the 5 reference, as that's where a copy to the 4 reference is stored. For abstract superclasses 3 this is fine as you are guaranteed a subclass, but 2 below that you have to be careful how you 1 handle the data.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.