Access Modifiers for Classes or Interfaces in Java
Access modifiers in Java are used to control the visibility of the variables, classes and methods within a class or package. There are different types of access modifiers that are used to define the accessibility in different ways. The access modifiers strictly enforce the level of accessibility and these are defined by different keywords.
Example: Access modifiers with interface and classes.
// Package-private interface
interface Display {
// Public method accessible anywhere
public void showPublic();
// Default method Accessible to any class that implements the interface
default void showDefault() {
System.out.println("Method in the private interface.");
}
}
// Package-private Class implementing the interface
class AccessModifiers implements Display {
// Public method implementation accessible anywhere
public void showPublic() {
System.out.println("This is a public method.");
}
// Protected method (accessible within the same package and subclasses)
protected void showProtected() {
System.out.println("This is a protected method.");
}
// Private method (accessible only within this class)
private void showPrivate() {
System.out.println("This is a private method.");
}
// default method (accessible within the same package)
void showPackagePrivate() {
System.out.println("This is a package-private method.");
}
// Public method to demonstrate access to private,protected, and package-private methods
public void demonstrateAccess() {
// Accessible within the class
showPrivate();
// Accessible within the class
showProtected();
// Accessible within the class
showPackagePrivate();
}
}
// Public class (file must be named Geeks.java)
public class Geeks {
public static void main(String[] args) {
// Create an instance of AccessModifiers
AccessModifiers obj = new AccessModifiers();
// Access public method
obj.showPublic();
// Access default method from the interface
obj.showDefault();
// Demonstrate access to private, protected, and package-private methods
obj.demonstrateAccess();
}
}
Output
This is a public method. Method in the private interface. This is a private method. This is a protected method. This is a package-private method.
Explanation:
- Only one top-level class (Geeks) is declared public to match the file name Geeks.java, while Display and AccessModifiers remain package-private, which is valid in Java.
- The program demonstrates all access modifiers: public, protected, private and default (package-private) within the AccessModifiers class.
- Display interface uses a default method, introduced in Java 8, allowing interfaces to contain method implementations.
- The demonstrateAccess() method showcases internal access to private, protected and package-private methods from within the same class.
- AccessModifiers implements the Display interface and the main() method uses polymorphism to call interface and class methods.
Different Access Modifiers
1. public Access Modifier
The public modifier provides the highest access among all the modifiers. We can access it anywhere in the package. The public modifier allows to use the variable, methods and class to acess in the same package.
Example: Demonstration of public access modifier in Java.
import java.io.*;
// public class
public class Geeks
{
// public method
public static void main (String[] args) {
System.out.println("Hello Geeks");
}
}
Output
Hello Geeks
2. protected Access Modifier
The protected modifier in Java allows the class, method and variable to Accessible within the same package and subclasses.
Example: Demonstrating the protected modifier in Java.
class BaseClass {
// prptected method
protected void Msg() {
System.out.println("This is the protected method.");
}
}
public class Geeks{
public static void main(String []args){
// Creating the object of BaseClass
BaseClass obj = new BaseClass();
// Calling the protected method from base class
obj.Msg();
}
}
Output
This is the protected method.
3. private Access Modifier
The private access modifier is used with method, class and variable and if the it can be only accessible in the class where it is defined.
Example: Demonstrating the private access modifier.
class BaseClass {
// Private variable
private String msg = "Hello Geeks";
// public method having private varialbe
public void Msg() {
System.out.println("Private variable having message: "+ msg);
}
}
public class Geeks {
public static void main(String []args){
// Creating the object of BaseClass
BaseClass obj = new BaseClass();
//Calling the method from base class
obj.Msg();
}
}
Output
Private variable having message: Hello Geeks
4. default Access Modifier
When the modifier is not define then it automatically specified default method or package private. The default having the accessibility within the same package. If the the variable, method or class having no modifier then it considered as defualt modifier
Example: Demonstrating the default modifier in Java
class BaseClass {
// default method
void Msg() {
System.out.println("This method having defualt modifier.");
}
}
public class Geeks
{
public static void main(String []args){
// Creating the object of BaseClass
BaseClass obj = new BaseClass();
// Calling the method having defualt modifier from base class
obj.Msg();
}
}
Output
This method having defualt modifier.
Access Modifiers Accessibility Level For Classes And Interface
Modifier | Class | Interface |
---|---|---|
public | Accessible from anywhere from different classes. | Similar to Class, accessible anywhere from different classes. |
default | It is only accessible within the same package. | Accessible within the same package. |
protected | Accessible within the same package and subclasses. | Not applicable to top-level interfaces. |
private | Accessible only in the class where it is defined | Not applicable to top-level interfaces and accessible in the same class or interface where it is defined. |