|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Access Control |
| 4 | +--- |
| 5 | + |
| 6 | +__Modifiers__ fall into _two_ categories: |
| 7 | + |
| 8 | +* __Access modifiers__: `public, protected, private and default (package-level access)`. |
| 9 | +* __Non-access modifiers__: `transient, synchronized, native, strictfp, final, abstract and static`. |
| 10 | + |
| 11 | +### Access Modifiers |
| 12 | + |
| 13 | +Two __types of access__ are there: |
| 14 | + |
| 15 | +* Whether method code in one class can access a member of another class |
| 16 | +* Whether a subclass can inherit a member of its superclass |
| 17 | + |
| 18 | +A **default member** may be accessed only if the class accessing the member belongs to the same package, |
| 19 | +whereas a **protected member** can be accessed by a subclass in the same package _(through dot operator and inheritance)_ and even if it is |
| 20 | +is in a different package _(through inheritance only)_. |
| 21 | + |
| 22 | +You cannot access a protected member using the dot (.) operator in the |
| 23 | +subclass **if the subclass is in a different package** from the parent class. |
| 24 | + |
| 25 | +The following code snippet makes it clear: |
| 26 | + |
| 27 | +{% highlight java %} |
| 28 | +package certification; |
| 29 | +public class Parent { |
| 30 | + protected int x = 9; // protected access |
| 31 | +} |
| 32 | + |
| 33 | +package other; |
| 34 | +import certification.Parent; |
| 35 | +class Child extends Parent { |
| 36 | + public void testIt() { |
| 37 | + System.out.println("x is " + x); // No problem; Child |
| 38 | + // inherits x |
| 39 | + Parent p = new Parent(); // Can we access x using the |
| 40 | + // p reference? |
| 41 | + System.out.println("X in parent is " + p.x); // Compiler |
| 42 | + // error |
| 43 | + } |
| 44 | +} |
| 45 | +{% endhighlight %} |
| 46 | + |
| 47 | +#### The below table gives the picture of all access modifiers: |
| 48 | + |
| 49 | +Visibility | Public | Protected | Default | Private |
| 50 | +------------------------------------------------- | -------- | -------------------------------- | --------- | --------- |
| 51 | +From the same class | Yes | Yes | Yes | Yes |
| 52 | +From any class in the same package | Yes | Yes | Yes | No |
| 53 | +From a subclass in the same package | Yes | Yes | Yes | No |
| 54 | +From a subclass outside the same package | Yes | Yes, through inheritance | No | No |
| 55 | +From any non-subclass class outside the package | Yes | No | No | No |
| 56 | + |
| 57 | +### Non-Access Modifiers |
| 58 | + |
| 59 | +**Final** is the only modifier which can be applied to local variables. It can also be used in method arguments like: |
| 60 | +{% highlight java %} |
| 61 | +// final in method arguments, can't be altered inside the method |
| 62 | +public Record getRecord(int fileNumber, final int recordNumber) {} |
| 63 | +{% endhighlight %} |
| 64 | + |
| 65 | +A method can never, ever, ever be marked as both **abstract and final, or both abstract and private**. |
| 66 | + |
| 67 | +#### Comparison of modifiers on variables vs. methods: |
| 68 | + |
| 69 | +Local Variables | Non-local Variables | Methods |
| 70 | +------------------ | ----------------------- | -------- |
| 71 | +final | final | final |
| 72 | + | public | public |
| 73 | + | protected | protected |
| 74 | + | private | private |
| 75 | + | static | static |
| 76 | + | transient | abstract |
| 77 | + | volatile | synchronized |
| 78 | + | | strictfp |
| 79 | + | | native |
| 80 | + |
0 commit comments