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.
// Java program demonstrating access modifiers
// with a class and an interface
// Note: A .java file can have only one public top-level class or interface,
// and it must match the name of the file. Other classes or interfaces cannot be declared public.
// Public interface accessible anywhere
interface Display {
// Public method accessible anywhere
public void showPublic();
// Default method (introduced in Java 8)
// is accessible to any class that implements the interface,
// regardless of the package.
default void showDefault() {
System.out.println("Method in the interface.");
}
}
// Public 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.");
}
// Package-private default method (accessible within the same package)
void showPackagePrivate() {
System.out.println("This is a package-private method.");
}
// Public method to demonstrate calling private,
// protected, and package-private methods
public void demonstrateAccess() {
// Accessible within the class
showPrivate();
// Accessible within the class
showProtected();
// Accessible within the class
showPackagePrivate();
}
}
// Main class
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 interface. This is a private method. This is a protected method. This is a package-private method.
Explanation: In the above code, we have different classes and interfaces, and we can execute them in the local system to check their accessibility level one by one. For better understanding, refer to the table mentioned below explaining the accessibility level of modifiers with classes and interface.
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.
// Demonstrating the pubic 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.
// 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.
// Demonstrating the private modifier in Java
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
// 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. |
Important Points:
- The top-level classes and interface and nested classes have different accessibility levels, as the .java file cannot be directly declared private or protected.
- The class and interface are accessible only within the same package.
- The .java file can have only one public top-level class or interface, and it must have the same name as the file.
- We cannot declare a class/interface with private or protected access modifiers.
- The nested interface which are created inside a class or inside another interface can have different modifiers the top level interfaces have some limitations of the modifiers they use. But nested interface have flexibility to use.