top of page
Search

Access modifiers in JAVA

Writer's picture: Titash RoyTitash Roy

Introduction JAVA provides a wide scope of accessibility to variables, methods, classes or constructors. We can change the access level of fields, constructors, methods and class by applying the access modifiers on it. There are 4 different types of access modifiers in JAVA.

  • default: Visible to the package. No modifiers are needed.

  • private: Visible to the class only.

  • public: Visible to the world.

  • protected: Visible to the package and all subclasses.

We will discuss about the 4 access modifiers in detail in this post.


private modifier Methods, variables, and constructors that are declared private can only be accessed within the class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private if they are to be accessed by other classes. Variables that are declared private can be accessed outside the class, if public getter/setter methods are present in the class. This is how encapsulation is achieved in JAVA. NOTE: If we make any class constructor private, we cannot create the instance of that class from outside the class.

In the below code, to access the variable a, we need to do it through getA and setA methods. If we try to access variable a or the method twice() directly from other classes, we will get compile-time error.

public class testaccess {
	private int a = 10;
	public int getA() {
		return a;
	}
	public void setA(int a) {
		this.a = a;
	}
	@Override
	public String toString() {
		return "testaccess [a=" + a + "]";
	}
	private int twice(int a) {
		return a*a;
	}
}

default modifier The access level of a default modifier is only within the package and cannot be accessed from outside the package. If we do not specify any access level, it will be the default. It provides more accessibility than private. But, it is more restrictive than protected, and public. In the below snippet, class A and class B are in different packages. When we try to access the class A or try to make an instance of class A in class B, we get compile time error. Even if we create class B extending class A(as a child of class A), but in a different package, it will result in compile time error. NOTE: The fields in an interface are implicitly public static final and the methods in an interface are by default public

package package1;  
class A{  
  void msg(){System.out.println("Hello");}  
}  
  
package package2;  
import package1.A;  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}  

protected modifier The protected modifier is accessible within the package and outside the package but through inheritance only. The protected access modifier can be applied to the variables, methods and constructors. It can't be applied to the class or the interface. It provides more accessibility than the default modifier. NOTE: Methods and fields in a interface cannot be declared protected. In the below snippet, class B is extended from class A and hence can access the message method, even after being in a different package.

package package1;  
public class A{  
  protected void msg(){System.out.println("Hello");}  
}  
  
package package2;  
import package1.A;  
public class B extends A{  
  public static void main(String args[]){  
   B obj = new B();  
   obj.msg();  
  }  
}  

public modifier A class, method, constructor, interface, etc. declared public can be accessed from anywhere in the Java application. However, if the public class is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. This modifier has the broadest scope. NOTE: The main() method of a JAVA application has to be public. Otherwise, it won't be visible by the JVM, to run the class.

package package1;  
public class A{  
  public void msg(){System.out.println("Hello");}  
}  
  
package package2;  
import package1.A;  
public class B extends A{  
  public static void main(String args[]){  
   B obj = new B();  
   obj.msg();  
  }  
}  

Summary In this post, we have learnt about the access modifiers and their scope in JAVA. To learn about access modifiers scope in terms of method overloading, click here. NOTE: Private methods can't be overloaded. The scope of access modifiers is Public>Protected>Default>Private. Public having the widest and private having the narrowest scope.

22 views0 comments

Recent Posts

See All

Comments


bottom of page