Followers

coreJava : Access Modifiers in Java

Access Modifiers in Java
The access modifiers in java specifies scope of a data member, method, constructor or class.
there are 4 types of java access modifiers :
*       private
*       default
*       protected
*       public
Understanding all java access modifiers
Let's understand the access modifiers by a simple table.
Access Modifier
within class
within package
outside package
outside package by subclass only
Private
Y
N
N
N
Default
Y
Y
N
N
Protected
Y
Y
N
Y
Public
Y
Y
Y
Y

Access control modifier

Java language has four access modifier to control access levels for classes, variable methods and constructor.
1.Default - Default has scope only inside the same package
2.Public - Public scope is visible everywhere
3.Protected - Protected has scope within the package and all sub classes
4.Private - Private has scope only within the classes

save as A.java

package com.mypack1;
//All data members are accessible within the class.

 public class A {
           
           public int i =10;
           protected int k =11            
           int l=12;
           private int j=13;              
            public void m1(){               }     
            protected void m2(){                    }     
            void m3(){              }     
            private void m4(){    }     
                      
          public static void main(String[] args) {
                   A a1 = new A();
                  
                   a1.m1();a1.m2();a1.m3();a1.m4();
                   System.out.println(a1.i+" "+a1.j+ " "+a1.k+" "+a1.l);
          }

}

save as  C.java

package com.mypack1;
import com.mypack1.*;
//private members are not accessible from other classes.

public class C {

          public static void main(String[] args) {
                  
                  
                   A a1 = new A();
                   a1.m1();
                   a1.m2();
                   a1.m3();
                   // a1.m4();
                   //System.out.println(a1.j);
                   System.out.println(a1.i);              
                   System.out.println(a1.k);
                   System.out.println(a1.l);
          }

}

save as B.java

package com.mypack2;
import com.mypack1.A;
/*  private,default and protected members are not accessible from other packages.
 *  but protected members are accessible from sub class objects.
 *
 */

public class B extends A{
          public static void main(String[] args) {
                   A a1 = new A();
                   a1.m1();                
                   //a1.m2();
                   //a1.m3();
                   // a1.m4();
                   //System.out.println(a1.j);
                   System.out.println(a1.i);              
                   //System.out.println(a1.k);
                   //System.out.println(a1.l);
                   B b1 = new B();
                  
                   b1.m1();                
                   b1.m2();
                   //b1.m3();
                   // b1.m4();
                   //System.out.println(b1.j);
                   System.out.println(b1.i);              
                   System.out.println(b1.k);
                   //System.out.println(b1.l);
          }

}

save as D.java

package com.mypack2;
//  private,default and protected members are not accessible from other packages.

import com.mypack1.A;


public class D {

          public static void main(String[] args) {

                   A a1 = new A();
                   a1.m1();                
                   //a1.m2();
                   //a1.m3();
                   // a1.m4();
                   //System.out.println(a1.j);
                   System.out.println(a1.i);              
                   //System.out.println(a1.k);
                   //System.out.println(a1.l);

          }

}

save as A.java

package com.mypack3;
//All data members are accessible within the class.

 public class A {
            public A(){}
            protected A(int i){}
            A(int i,int j){}
            private A(int i,int j,int k){}
           
          public static void main(String[] args) {
                   A a1 = new A();
                   A a2 = new A(11);
                   A a3 = new A(11,22);
                   A a4 = new A(11,22,33);
                  
          }

}

save as  C.java

package com.mypack3;
import com.mypack3.*;
//private members are not accessible from other classes.

public class C {

          public static void main(String[] args) {
                   A a1 = new A();
                   A a2 = new A(11);
                   A a3 = new A(11,22);
                   //A a4 = new A(11,22,33);
                  
                  
          }

}

save as B.java

package com.mypack4;
import com.mypack3.A;
/*  private,default and protected members are not accessible from other packages.
 *  but protected members are accessible from sub class objects.
 *
 */

public class B extends A{
          B(int i){
                   super(i);
                   }
          public static void main(String[] args) {
                   A a1 = new A();
                   //A a2 = new A(11);
                   //A a3 = new A(11,22);
                   //A a4 = new A(11,22,33);
                  
                   B b1 = new B(11);            
                  
          }

}

save as D.java

package com.mypack4;
//  private,default and protected members are not accessible from other packages.

import com.mypack3.A;

public class D {

          public static void main(String[] args) {
                   A a1 = new A();
                   //A a2 = new A(11);
                   //A a3 = new A(11,22);
                   //A a4 = new A(11,22,33);
                  
                  

          }

}

No comments:

Post a Comment