Reputation: 393
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;
}
}
Upvotes: 9
Views: 451
Reputation: 1075755
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.
Upvotes: 7