Loading...

Access Control in Java

java tech
Ram Patra Published on May 22, 2019
Image placeholder

Modifiers fall into two categories:

  • Access modifiers: public, protected, private and default (package-level access).
  • Non-access modifiers: transient, synchronized, native, strictfp, final, abstract and static.

Access Modifiers

Two types of access are there:

  • Whether method code in one class can access a member of another class
  • Whether a subclass can inherit a member of its superclass

A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed by a subclass in the same package (through dot operator and inheritance) and even if it is in a different package (through inheritance only).

You cannot access a protected member using the dot (.) operator in the subclass if the subclass is in a different package from the parent class.

The following code snippet makes it clear:

package certification;
public class Parent {
    protected int x = 9; // protected access
    
    protected int getX() {
        return x;
    }
}

package other; // different package
import certification.Parent;
class Child extends Parent {
    public void testIt() {
        System.out.println("x is " + x); // No problem; Child
                                         // inherits x
        Parent p = new Parent(); // Can we access x using
                                 // p reference?
        System.out.println("X in parent is " + p.x); // Compiler
                                                     // error
        System.out.println("X in parent is " + p.getX()); // Compiler
                                                          // error
    }
}

NOTE: If Child class would have been in the same package as Parent then there would be no compiler error.

The below table gives the picture of all access modifiers:

Visibility Public Protected Default Private
From the same class Yes Yes Yes Yes
From any class in the same package Yes Yes Yes No
From a subclass in the same package Yes Yes Yes No
From a subclass outside the same package Yes Yes, through inheritance No No
From any non-subclass class outside the package Yes No No No

Non-Access Modifiers

Final is the only modifier which can be applied to local variables. It can also be used in method arguments like:

// final in method arguments, can't be altered inside the method
public Record getRecord(int fileNumber, final int recordNumber) {}

Final when applied to a method prevents it to be overridden and when applied to a class makes it un-inheritable i.e, it can never be subclassed (no class can extend it).

An abstract method is a method that’s been declared (as abstract) but not implemented. In other words, the method contains no functional code. And the class which contains at least one of such methods is an abstract class and has to be declared abstract. An abstract class can never be instantiated.

A method can never, ever, ever be marked as both abstract and final, or both abstract and private or both abstract and static.

A class having even a single abstract method has to be declared abstract or if it extends an abstract class then it must implement all abstract methods of the superclass otherwise you have make it abstract as well.

Comparison of modifiers on variables vs. methods:

Local Variables Non-local Variables Methods
final final final
  public public
  protected protected
  private private
  static static
  transient abstract
  volatile synchronized
    strictfp
    native
Ram Patra Published on May 22, 2019
Image placeholder

Keep reading

If you liked this article, you may like these as well

gadget unboxing monitor September 3, 2020 Unboxing of my new LG Ultragear 27GN950-B monitor

I ordered the LG Ultragear 27GN950 from Amazon.de. I compared all the Amazon EU websites and it was the cheapest in Germany. When I ordered it showed that it would deliver in 1-2 months but luckily it came back in stock in a day or two and was dispatched right after. I got the monitor delivered to Ireland within 4-5 days.

January 5, 2022 Two simple ways to earn interest with your crypto

There are many ways you can make your crypto savings work for you but I am going to list down two of the most well-known ones that I personally use most frequently.

February 6, 2022 My latest PC Build in 2022

I wrote a post in the past describing my crypto mining plus gaming pc and after around 2-3 years of using it, I decided to add more GPUs to it and totally dedicate it to mining crypto. Ergo, I needed a new PC for my gaming and other day-to-day tasks apart from app development. App development I do only on my mac. In this post, I would be describing all the components I used in my PC and why I chose them over others.