Saturday, June 12, 2021

Java Tutorial: Access Modifiers

Chapters

Java Access Modifiers

As the title implies, access modifiers restrict classes and class members accessibility in packages or enclosing blocks. There are four access modifiers in java. Let's discuss them one by one.

public Access Modifier

public access modifier is the most unrestrictive modifier in all access modifiers in java. classes or class members that have this modifier are accessible to all packages in a project and related enclosing blocks.
public class SampleClass{
  public String name = "SampleClass";
  
  public void displayName(){
    //name is accessible here
    System.out.println(name);
  }
  
  public static void main(String[]args){
   //can't directly put non-static variable in a
   //static context even they're in the same
   //class block
   //System.out.println(name);
   
   SampleClass sc = new SampleClass();
   //variables and other public members
   //can be accessed once there's an 
   //instance of a class
   System.out.println(sc.name);
   sc.displayName();
   
  }
}
Package accessibility. We need to put source files in folders.
//This source file must be in p1 folder
package p1;

public class Compute{

  public int add(int n1, int n2){
    return n1 + n2;
  }
}

//This source file must be in p2 folder
package p2;

//use import keyword to import classes
//to another package
import p1.Compute;

public class SampleClass{

  public static void main(String[]args){
    //compute class is accessible in p2
    Compute compute = new Compute();
    System.out.println("Add 3 and 7");
    
    //add() is also accessible in p2
    System.out.println("Answer: " + compute.add(3,7));
  }
}
default Access Modifier

This modifier is keyword-less modifier. If we want a class or a class member to have default modifier, just leave the modifier part blank. classes or class members that have this modifier are only accesible in the package where they reside.
public class SampleClass{
  String name = "SampleClass";
  
  void displayName(){
    //name is accessible here
    System.out.println(name);
  }
  
  public static void main(String[]args){
   //can't directly put non-static variable in a
   //static context even they're in the same
   //class block
   //System.out.println(name);
   
   SampleClass sc = new SampleClass();
   //variables and other default members
   //can be accessed once there's an 
   //instance of a class
   System.out.println(sc.name);
   sc.displayName();
   
  }
}
Package accessibility. We need to put source files in folders.
//This source file must be in p1 folder
package p1;

public class Compute{
  int aggrResult;
  
  public int add(int n1, int n2){
    aggrResult += (n1 + n2);
    return n1 + n2;
  }
}

class Calculate{

  public int add(int n1, int n2){
    return n1 + n2;
  }
}

//This source file must be in p2 folder
package p2;

//use import keyword to import classes
//to another package
import p1.Compute;
import p1.Calculate;

public class SampleClass{

  public static void main(String[]args){
    //compute class is accessible in p2
    Compute compute = new Compute();
    System.out.println("Add 3 and 7");
    
    //add() is also accessible in p2
    System.out.println("Answer: " + compute.add(3,7));
    System.out.println("Add 4 and 6");
    System.out.println("Answer: " + compute.add(4,6));
    
    //error: aggrResult is not accessible in another
    //package because it has default access modifier
    //System.out.println("Aggregate Result: " + 
    //                   compute.aggrResult);
                       
    //error: Calculate class is not accessible in another
    //package because it has default access modifier
    //Calculate calculate = new Calculte();
  }
}
protected Access Modifier

This access modifier makes classes and class members only accessible in a package where they reside or in a sub class
Note: protected access modifier is not allowed in top-level classes.
public class SampleClass{
  
  public static void main(String[]args){
   ClassA a = new ClassA();
   //name variable with protected modifier
   //can be accessed in the package where
   //it resides
   System.out.println(a.name);
  }
}

class ClassA{
  protected String name = "ClassA";
  
  public String getName(){
    return name;
  }
}

class ClassB extends ClassA{

  public ClassB(){
    //name is accessible here because
    //ClassB is sub class of ClassA
    //and ClassB is in the same package
    //as ClassA
    name = "ClassB";
  }
}
Package accessibility. We need to put source files in folders.
//This source file must be in p1 folder
package p1;

public class ClassA{
  protected String name = "ClassA";
  
  public String getName(){
    return name;
  }
}

//This source file must be in p1 folder
package p1;

public class ClassB extends ClassA{

  public ClassB(){
    //name is accessible here because
    //ClassB is sub class of ClassA
    //and ClassB is in the same package
    //as ClassA
    name = "ClassB";
  }
}

//This source file must be in p2 folder
package p2;

import p1.ClassA;
import p1.ClassB;

public class SampleClass{
  
  public static void main(String[]args){
   ClassA a = new ClassA();
   //error: name variable can't be accessed in
   //another package because it has
   //protected access modifier
   //System.out.println(a.name);
   
   ClassC c = new ClassC();
   System.out.println(c.getName());
  }
}

class ClassC extends ClassA{
  
  ClassC(){
  //name variable can be accessed here
  //because ClassC is sub class of ClassA
   name = "ClassC";
  }
}
private Access Modifier

private access modifier is the most restrictive modifier in all access modifiers in java. Classes and class members that have this modifier are only accessible in their enclosing class.
Note: private access modifier is not allowed in top-level classes.
public class SampleClass{
  private String name;
  private static int num;
  
  SampleClass(String name, int num){
    //name variable can be accessed here
    //because this constructor block is
    //part of the instance of SampleClass
    this.name = name;
    
    //static variable is allowed in 
    //non-static context
    SampleClass.num = num;
  }
  
  public static void main(String[]args){
    SampleClass sc = new SampleClass("SampleClass",1);
    
    //name and num variable can be accessed here
    //because main() is in SampleClass enclosing block.
    System.out.println(sc.name);
    System.out.println(SampleClass.num);
  }
}

class ClassA{
  private int num;
  
  ClassA(){
    //error: num variable in SampleClass
    //can't be accessed here because this
    //is not SampleClass class.
    //num = SampleClass.num;
  }
}

class ClassB extends ClassA{

  ClassB(){
    //can't access num of ClassA here
    //because this is not part of
    //ClassA
    //num = 2;
  }
}
Tips On Using Access Modifiers

When building an application, we want to balance its security and maintainability. If we want our application to be more maintainable then, we need to relax some of its restrictions so that, it has more flexibility, thus, it can be easily maintained. However, relaxing restrictions may reduce application's security.

If we want to make our applcation to be more secure then, we need to increase the restrictions of our application. However, It will make our application less flexible, thus, it may become less maintanable.

No comments:

Post a Comment