EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Access Modifiers

Access Modifiers in Java

In Java, access modifiers are used to control the visibility and accessibility of classes, methods, and variables. By using these modifiers, we provide the JVM with information like whether a class can be accessed from outside its package, whether child classes can be created, and whether object instantiation is allowed.

Modifier 1: Public Access Modifier

When a class is declared as public, it can be accessed from anywhere. The same applies to methods and variables declared as public within that class.

Example:

// Creating a package
package pack1;

// Declaring a public class
public class MyClass1 {

    // Declaring a public method
    public void display() {
        System.out.println("Public Access Modifier Example");
    }
}

In another package, you can import this class and use it:

// Creating a package
package pack2;

// Importing the class from pack1
import pack1.MyClass1;

public class Main {
    public static void main(String[] args) {
        // Creating an object of class MyClass1
        MyClass1 obj = new MyClass1();
        // Calling the public method
        obj.display();
    }
}

Output:

Public Access Modifier Example

If MyClass1 was not public, you would receive a compile-time error stating that MyClass1 is not accessible from another package.

Modifier 2: Protected Access Modifier

The protected modifier is applicable to data members, methods, and constructors but not for top-level classes or interfaces. When a member is declared as protected, it is accessible within the same package and in subclasses of other packages.

Example:

package pack1;

// Declaring a parent class
class MyClass2 {

    // Declaring a protected method
    protected void show() {
        System.out.println("Protected Access Modifier Example");
    }
}

Now, in another package, we can access it through inheritance:

package pack2;

// Importing the class from pack1
import pack1.MyClass2;

class ChildClass extends MyClass2 {
    public static void main(String[] args) {
        // Creating an instance of the child class
        ChildClass obj = new ChildClass();
        // Accessing the protected method
        obj.show();
    }
}

Output:

Protected Access Modifier Example

We can access the protected method in a subclass even from a different package, but not directly from a non-child class in another package.

Modifier 3: Private Access Modifier

The private modifier restricts access to members within the same class only. Neither child classes nor other classes, even within the same package, can access private members.

Example:

// Defining a class
class MyClass3 {

    // Declaring a private method
    private void secret() {
        System.out.println("Private Access Modifier Example");
    }

    public void accessPrivate() {
        // Accessing the private method within the same class
        secret();
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass3 obj = new MyClass3();
        // Accessing the public method
        obj.accessPrivate();
    }
}

Output:

Private Access Modifier Example

Modifier 4: Default (Package) Access Modifier

When no access modifier is specified, the default access level (also called “package-private”) applies. This means that the class or its members are accessible only within the same package and not from other packages.

Example:

// Defining a class with default access
class MyClass4 {

    // Declaring a default-access method
    void displayMessage() {
        System.out.println("Default Access Modifier Example");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of MyClass4
        MyClass4 obj = new MyClass4();
        // Calling the default-access method
        obj.displayMessage();
    }
}

Output:

Default Access Modifier Example

Summary of Differences Between Access Modifiers

Access Modifiers
ModifierApplicabilityAccessibility From Same PackageAccessibility From Different PackageAccessibility from Subclass Outside PackageAccessibility from Non-subclass Outside Package
PublicClasses, Methods, FieldsYesYesYesYes
ProtectedMethods, FieldsYesNoYes (only in subclasses)No
PrivateMethods, FieldsNoNoNoNo
Default (Package)Classes, Methods, FieldsYesNoNoNo
End of lesson.