Monday, 8 February 2016

Access Modifiers public, protected, private and default in JAVA

In java we have 4 types of Access modifiers

 1. Public: Public member can be accessed from anywhere within the package and outside package

In the below example we are accessing public member which was declared in package pack1 in package pack2

        Pack1Program1.java

package pack1;
public class Pack1Program1 {
       public int i=10; //public variable
              public static void main(String[] args) {
                     System.out.println("Pack1Program1");
              }
}

          Pack2Program1.java
      
package pack2;
import pack1.Pack1Program1;//importing
public class Pack2Program1 {
              public static void main(String[] args) {
              //for accessing Pack1Program1 class public member i we should create an object as this is instance variable
                     Pack1Program1 pp1 = new Pack1Program1();
                     System.out.println(pp1.i);//accessing public member
             
              }
}
 2. Protected: Protected member can be accessed with in the package and outside package also with one condition. ie., outside package class should be child class of class which is having protected member and by using child class object only we are allowed to access.

Example:

        Pack1Program1.java

package pack1;
public class Pack1Program1 {
       public int i=10; //public variable
              public static void main(String[] args) {
                     System.out.println("Pack1Program1");
              }
}

        Pack2Program1.java
package pack2;
import pack1.Pack1Program1;//importing
public class Pack2Program1 extends Pack1Program1{ // extending Pack1Program1 to Pack2Program1
              public static void main(String[] args) {
              //for accessing Pack1Program1 class protected member 'i' we should create an object for child class
              //and by using child class reference we can access protected member
              Pack2Program1 pp2 = new Pack2Program1();
              System.out.println(pp2.i);
             
       }
}
 3. default: Actually there is no keyword for default access modifier. If we don’t mention ant access modifier for the class members those members are have default access modifier. Default member can be accessible with in the package.
Example:
                package pack1;
public class Pack1Program1 {
              int i=10; //if we are not mention any access modifier then it will become default
                     public static void main(String[] args) {
                           Pack1Program1 pp1 = new Pack1Program1();
                           System.out.println(pp1.i);
                     }

4.Private:  private member we can access with in the block. All variables inside method and parameters in a method are having private accessibility
Example:
                package pack1;
public class Pack1Program1 {
              public static void main(String[] args) {
                     Pack1Program1 pp1 = new Pack1Program1();
                     System.out.println(pp1.j);// Compilation Error : j cannot be resolved or is not a field
              }
              public void methodOne(){
                     int j=20; //j is private for methodOne
              }

}

No comments:

Post a Comment