Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the use of less restrictive member access modifiers than the class access modifier?

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.

  • Is my understanding correct?
    • If not, what are the consequences?
  • What could be valid reasons to have less restrictive member access modifiers?
  • Finally, what are there best practice to follow?

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.
  • Then pizzas.C calls apples.B.bla to obtain a reference to A.foo and calls it.
  • So 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;
    }
}

like image 437
Stefan D. Avatar asked Oct 08 '19 10:10

Stefan D.


People also ask

Why do we use different access modifiers?

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.

Which access modifier is the most restrictive and why?

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.

What access modifier is the least restrictive?

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.

Which access modifier would you use if you wanted the most restrictive access to a member?

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.


1 Answers

Is my understanding correct?

Yes.

What could be valid reasons to have less restrictive member access modifiers?

Two reasons:

  • Sometimes, you're implementing an interface; interface methods must be public
  • It makes it easier to change the overall access of your class. For instance, if you mark all the methods that you'd ever want to be 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.

like image 127
T.J. Crowder Avatar answered Nov 12 '22 09:11

T.J. Crowder