Say I have a class with some members, and the members have a less restrictive access modifier than the class itself.
A concrete example could be:
package apples;
class A { // package private
public int foo() { // public (=> less restrictive than *package private*)
return 42;
}
}
To my understanding a class access modifier that is more restrictive than the member access modifier, will override the less restrictive member access modifiers. So a less restrictive member access modifier should have no effect at all.
I also did some experimenting because I thought it might have consequences once I start passing function references around, however even then the access modifier does not seem to matter.
The situation that I constructed is the following:
apples.B
provides a public method bla()
that returns a reference to apples.A.foo
.pizzas.C
calls apples.B.bla
to obtain a reference to A.foo
and calls it.A.foo()
is not directly visible to C
, but is only indirectly accessible via B.bla()
I tested it and it does not make a difference whether I make the access modifier of foo()
package private or not.
package apples;
import java.util.function.IntSupplier;
public class B {
public IntSupplier getReferenceToAFoo() {
A aInstance = new A();
return aInstance::foo;
}
}
package pizzas;
import apples.B;
import java.util.function.IntSupplier;
public class C {
private int callAFooIndirectly() {
B bInstance = new B();
IntSupplier intsupplier = bInstance.getReferenceToAFoo();
return intsupplier.getAsInt();
}
public static void main(String[] args) {
C cInstance = new C();
int i = cInstance.callAFooIndirectly();
System.out.println(i);
assert 42 == i;
}
}
Access modifiers are mainly used for encapsulation. It can help us to control what part of a program can access the members of a class. So that misuse of data can be prevented.
Private. Any method, property or constructor with the private keyword is accessible from the same class only. This is the most restrictive access modifier and is core to the concept of encapsulation.
Java public access modifier: When applied to a class, the class is accessible from any classes regardless of packages. This is the least restrictive access modifier which means the widest range of accessibility, or visibility.
1.4. A private access modifier is the most restrictive access level. (Topmost) Classes and interfaces cannot be private. private members are accessible within the same class only. Methods, Variables, and Constructors that are declared private can only be accessed within the declared class itself.
Is my understanding correct?
Yes.
What could be valid reasons to have less restrictive member access modifiers?
Two reasons:
public
public
, even in a package-private class, then later all you have to do to make the class public is add public
to the class declaration.Finally, what are there best practice to follow?
That's a matter of opinion, so not well-suited to a Stack Overflow question or answer. Do what seems reasonable to you and/or your team, and/or do what your team's style guide tells you to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With