java

Java Access Modifier


What is access modifier?

In Java, an access modifier is a keyword that specifies the visibility or accessibility of a class, method, or field within the program.

Java provides four types of access modifiers:

 

  • private: Members (fields or methods) declared as private are only accessible within the same class. They cannot be accessed directly by other classes.

Syntax

public class MyClass {
    private int myPrivateField; // myPrivateField accessible within class only

    private void myPrivateMethod() {
        // Code here
    }
}

 

Example:

class MyClass {
    // Private field accessible only within the same class
    private int x;

    // Private method accessible only within the same class
    private void fun() {
        System.out.println("Executing private method!");
    }

    // Public method to access the private field indirectly
    public void show(int value) {
        // Accessing the private field within the same class
        x= value;
        System.out.println("Assigning private field to: " + x);
    }

    // Public method to access the private method indirectly
    public void calling() {
        // Accessing the private method within the same class
        fun();
    }

    public static void main(String[] args) {
        // Creating an instance of MyClass
        MyClass Obj = new MyClass();

        // Accessing the public methods to interact with private members
        Obj .show(42);
        Obj .calling();
    }
}

Output:

Assigning private field to: 42
Executing private method!

 

  • default: If no access modifier is specified, the default access level is package-private. Members with default access are visible to all classes in the same package but not to classes in different packages.

Syntax

class PackagePrivateClass {
    int packagePrivateField; // 

    void packagePrivateMethod() {
        // Code here
    }
}

 

Example

package LearnCoding;
class A {
    // Package-Private method
    void show() {
        System.out.println("Package-Private method");
    }
}

class B {
    public static void main(String[] args) {
        // Creating an instance of DefaultAccessExample
        A obj = new A();
        obj.show();
    }
}

 

Explanation

show() method is default method in above example.

show() method can accessible by other class also but within LearnCoding package only.

 

  • protected: Members declared as protected are accessible within the same package and by subclasses through inheritance (even if they are in different packages). 

Declaring protected Variables, Methods and Constructors

public class FirstClass {

    protected String name;

    protected FirstClass(String name) {
        this.name = name;
    }

    protected String getName() {
        return name;
    }
}

Accessing protected Fields, Methods, and Constructors
1. From the Same Package

creating a new GenericClass declared in the same package as FirstClass

public class GenericClass {

    public static void main(String[] args) {
        FirstClass first = new FirstClass("random name");
        System.out.println("FirstClass name is " + first.getName());
        first.name = "new name";
    }
}

2. From a Different Package

now try to interact with these fields from a class declared in a different package from FirstClass

public class SecondGenericClass {

    public static void main(String[] args) {
        FirstClass first = new FirstClass("random name");
        System.out.println("FirstClass name is "+ first.getName());
        first.name = "new name";
    }
}

we getting compilation errors:

The constructor FirstClass(String) is not visible
The method getName() from the type FirstClass is not visible
The field FirstClass.name is not visible

3. From a Sub-Class

public class SecondClass extends FirstClass {
    
    public SecondClass(String name) {
        super(name);
        System.out.println("SecondClass name is " + this.getName());
        this.name = "new name";
    } 
}

Explanation:

we can access all the protected fields, methods, and constructors. This is because SecondClass is a sub-class of FirstClass.

 

  • public: Members declared as public are accessible from any class. There are no restrictions on their accessibility.

Syntax

public class PublicClass {
    public int publicField;

    public void publicMethod() {
        // Code here
    }
}

 

Example

// Public class
public class PublicAccessExample {
    // Public variable
    public int publicVariable = 42;

    // Public method
    public void publicMethod() {
        System.out.println("Public method");
    }
}

public class AnotherClass {
    public static void main(String[] args) {
        // Creating an instance of PublicAccessExample
        PublicAccessExample example = new PublicAccessExample();

        // Accessing public variable and method from AnotherClass
        System.out.println("Public variable: " + example.publicVariable);
        example.publicMethod();
    }
}