Various access specifiers in Java with examples

Hello everyone! learn how to use different access specifiers in java with suitable examples. What are the access specifiers? So access specifiers are keywords which allow access to different classes, fields, methods of your java program. We know that the main feature of Java is encapsulation and this can be done mentioning various access specifiers. The main advantage of access specifiers is because we can restrict certain variables, methods to certain classes, methods, etc.

Also, learn:

Various access specifiers in Java

In java, we have 4 access specifiers

1. Public

2. Private

3. Protected

4. Default(no access specifier mentioned)

Let us learn about all four specifiers one by one with an example.

1.Public:

Public specifiers are meant to have the highest level of accessibility among all specifiers. If we declare class, method, fields, variables as public. As a result, these can be accessed by any other class, methods. In addition, there is a  limitation that the accessed class or method should belong to the same package.

public class Main(){
   public static void main(String[] args){
        public int x;
     }
}

2.Private:

Private specifiers are meant to have the lowest level of accessibility among all specifiers. If we declare class, method, fields, variables as private. And these can be accessed only within the class. This specifier is completely opposite to that of a public class.

public class Main(){
     public static void main(String[] args){
               private int x,y;
                x=10;
                y=10;
                sum(x,y);
     }
     private sum(int x,int y)
    {    int s=x+y;
          System.out.println(s);
     }
}

3.Protected:

We use protected access specifier to a method or a variable. If we declare a variable or method as protected. And then classes within the same package can access those variables and not outside the package.

package p1;
public class Main{
       protected void sum(int x,int y){
              int s=x+y;
          System.out.println(s);
     }
}
package p2;
import p1;
public class Pclass extends Main{
     public static void main(String[] args){
             Pclass o=new Pclass();
             o.sum(10,10);
     }
}

4.Default:

When none of the access specifiers is used. Then we take it as a default access specifier. So when you don’t mention any access specifier. Then we can access the methods, variables within the class and outside the class of the same package only.

package p1;
public class Main{
    public static void main(String[] args)
{
     System.out.println("Hellow world");
}
   void display(){
     System.out.println("hi");
   }
}
// another class
package p2;
public class Class2 extends Main{
    public static void main(String[] args){
        Class2 o=new Class2();
        0.display();
   }
}

In the above example, if we try to execute we will face an error.

Leave a Reply

Your email address will not be published. Required fields are marked *