Access Modifiers for Classes or Interfaces in Java
Last Updated :
17 Apr, 2025
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
// 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();
}
}
OutputThis 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.
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");
}
}
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.
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();
}
}
OutputThis 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.
Java
// 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();
}
}
OutputPrivate 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
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();
}
}
OutputThis 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.
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read