Access Modifiers || Java example

 

Java access modifiers :

In java we have Access Modifiers to provide access to methods, class, variables.We can alter the accessibility of the variables, methods and classes using these modifiers.

The access modifiers in java are

  1. Default
  2. Public
  3. Private
  4. Protected

 

Default :

When you don’t specify any access modifier by default it will be considered as default case.

int num;
String name;

Here int and String have default as access modifier.

The variables and methods specified as default are only accessed within the package in which they are declared.

 

Consider a package package1 in which a class with default variables and methods are specified as default i.e., by default

 

package package1;

 class Test {

    int num;
    String name;

    void display(){
        System.out.println("Message");
    }
}

when tried to access from same package i.e, package1

package package1;

public class ab {

    public static void main(String args[]) {

        Test a = new Test();

        a.num = 0;
        a.name = "comp";

        a.display();
    }

}

 

java access modifiers

 

 

when tried to access from another class in package2

java access modifiers

 

s

 

 

 

 

 

 

 

 

Public :

Variables, methods, classes specified as public can be accessed from any where out side the package.Considering the above example let’s make a public declaration and see what’s the output

 

package package1;

 public class Test {

   public int num;
   public String name;

    public void display(int num, String name){
        System.out.println("num "+num+"  name "+ name);
    }
}


 

now try to access them from another class in package2

package package2;

import package1.Test;

public class abcde {

    public static void main(String args[]){

        Test a = new Test();

        a.num = 1;
        a.name = "comp";

        a.display(a.num,a.name);
    }

}

 

java access modifiers

 

Private :

When private declaration is provided for variables, classes and methods we can access them within that class they are specified.

package package1;

public class Test {

    private int num;
    private String name;

    private void display(int num, String name) {
        System.out.println("num " + num + "  name " + name);
    }

    public static void main(String args[]){
        
        Test t = new Test();
        t.name = "comp";
        t.num = 1;
        
        t.display(t.num,t.name);
    }
}



 

java access modifiers

 

when tried to access from same package also we get error also the same for class outside the package.

 

Protected :

When protected is specified the variables and methods are accessible in the subsequent classes only as the class extends the class in which these variables and methods are present.

 

package package1;

public class Test {

    protected int num;
    protected String name;

    protected void display(int num, String name) {
        System.out.println("num " + num + "  name " + name);
    }

}



 

try to access them in subsequent class within same package i..e,

 

public class ab extends Test

 

package package1;

public class ab extends Test{

    public static void main(String args[]) {

        Test a = new Test();

        a.num = 1;
        a.name = "comp";

        a.display(a.num,a.name);
    }

}

 

 

 

Show Buttons
Hide Buttons
Read previous post:
Break || Continue || Java example

Java break and continue : The java keywords break and continue are used to stop a iteration or to skip...

Close