Home >>Java Tutorial >Java Access Modifiers
The access modifier in Java basically does the work of specifying the accessibility or scope of a constructor, class, field, or method. The access level of the fields, constructors, class, and methods can be changed by the programmer just by applying the access modifier on it. Apart from the access modifiers, there are generally two types of modifiers in the Java language that are: access modifiers and non-access modifiers.
As far as the Access Modifiers are concerned they are generally of four types in the Java Language as depicted below:
The above mentioned Java access modifiers are the parts or you can say the successors of the Java Modifiers. A modifier in Java can be simply understood by its use in the language. It is basically used in order to set the access level for methods, attributes, classes and constructors. There are basically two types of the modifiers in the Java language that are as depicted below:
Java provides a number of access modifiers In order to set the access levels for methods, classes, variables, and constructors, the Java language delivers various access modifiers that are depicted below:
There is numerous of non-access modifiers that are provided by the Java that are basically used to achieve various other functionalities that are depicted below:
The private access modifier acts just like its name and is accessible only within the class. Here is an example of the private access modifiers in which there are two classes A and Simple are created. In this example a class contains private data member and the private method. The motto of this example is to access these private members from the outside the class so that there is a compile-time error:
class A { private int data = 40; private void msg () { System.out.println ("Hello java"); } } public class Main { public static void main (String args[]) { A obj = new A (); System.out.println (obj.data); //Compile Time Error obj.msg (); //Compile Time Error } }
The Default access modifier possesses the access level that is only valid within the package. The code that has been written is only accessible in the same package. This modifier is generally used when the user don't specify a modifier.
Here is an example of the default access modifiers in the Java language in which the A class will be accessed from outside its package, as of the fact that A class is not public and it cannot be accessed from outside the package:
//save by A.java package pack; class A { void msg () { System.out.println ("Hello"); } } //save by B.java package mypack; import pack.*; class B { public static void main (String args[]) { A obj = new A (); //Compile Time Error obj.msg (); //Compile Time Error } }
The Protected access modifier has an access level that is valid within the package and outside the package via child class. This cannot be accessed from outside of the package, in case the programmer forgets to create the child class. The code will be accessible in the same package and the subclasses.
Here is an example of the protected access modifiers in the Java language in which A class of the pack package is public hence, it can be accessed from outside of the package as of the fact we know that msg method of this package is declared as the protected modifier hence; it can be accessed from outside the class only via inheritance:
//save by A.java package pack; public class A { protected void msg () { System.out.println ("Hello"); } } //save by B.java package mypack; import pack.*; class B extends A { public static void main (String args[]) { B obj = new B (); obj.msg (); } }
The Public access modifier has an access level that is valid for everywhere.
Here is an example of the public access modifiers, observe this example carefully to get to the concept from a depth:
//save by A.java package pack; public class A { public void msg () { System.out.println ("Hello"); } } //save by B.java package mypack; import pack.*; class B { public static void main (String args[]) { A obj = new A (); obj.msg (); } }