DEV Community

Buddhika Chathuranga
Buddhika Chathuranga

Posted on

Access Modifiers

In the Java programming language, we can see two main types of modifiers which are access modifiers and non-access modifiers.

Access modifiers

  • public
  • protected
  • default
  • private

Non-access modifiers

  • static
  • final
  • abstract
  • synchronized
  • volatile
  • transient
  • native
  • strictfp

In this article, I am going to talk about access modifiers.

Access modifiers are responsible to define the scope, to where elements such as methods, variables of a code are visible.

private

  • The private access modifier is accessible only from the class.
  • Private access modifier is not applicable for classes and interfaces
  • Applicable to methods, properties, and constructors
package temp;

class A{
    private int x;
}

class B{
    A a = new A();
    System.out.println(a.x);
}
Enter fullscreen mode Exit fullscreen mode

The above code has a syntax error. Because cannot access x from class B, since it is modified as private in class A. So x is accessible only from class A.

default

  • When the developer not defined access modifiers explicitly, that member has the default access modifier.
  • default access modifier is visible to all classes in the same package. So-called package-private
package temp;

class A{
    int x;
}

class B{
    A a = new A();
    System.out.println(a.x);
}
Enter fullscreen mode Exit fullscreen mode

Now in the above example, can access x from Class B. Because x is modified as default and A and B both classes are in the same package.

package temp;

class A{
    int x;
}


package temp2;

class C extends B{
    void m(){
        System.out.println(x);
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above code, even class C has extended class B, inside the class C cannot access x. Because x is modified as default and class trying to access x from a different package.

Protected

  • protected access modifier is visible to the same package
  • protected access modifier is also visible to other packages, but only via a subclass
  • applicable for constructor, method, and property
package temp;

class A{
    protected int x;
}


package temp2;

class C extends B{
    void m(){
        System.out.println(x);
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above example from the C class of package temp2 can access x which is defined in A class in package temp because x has the protected access modifier. Also, x will be accessible to the Child classes of C.

public

  • accessible to everywhere
  • applicable to classes, interfaces, properties, methods, constructors
package temp;

class A{
    public int x;
}


package temp;

class D{
    void method(){
        A a = new A();
        System.out.println(a.x);
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above code, x variable of the class A is accessible to everywhere of the code. So can access x from class D in the package temp2.

Comparison

public

  • within class : yes
  • within package : yes
  • outside package by inherit : yes
  • outside the package : yes

protected

  • within class : yes
  • within package : yes
  • outside package by inherit : yes
  • outside the package : no

default

  • within class : yes
  • within package : yes
  • outside package by inherit : no
  • outside the package : no

private

  • within class : yes
  • within package : no
  • outside package by inherit : no
  • outside the package : no

Top comments (0)