Access modifiers in Java

 Access Modifier Introduction:

Access modifiers in Java are keywords used to specify the visibility and accessibility of classes, methods, variables, and constructors within a Java program. They are essential for encapsulation and regulating access to various parts of a class. There are four types of access modifier in java :

1) public 

2) private 

3) protected

4) default

public : In Java, the public access modifier is the most permissive one. When applied to a class, method, variable, or constructor, it allows that component to be accessed from any other class in the same package or from any other package.

public class: A class declared as public can be accessed from any other class in the same package or from any other package. It is commonly used for classes that need to be widely accessible and form part of the API.

// MyClass.java (in package com.example)
package com.example;

public class MyClass {
    // Class implementation
}
public method: A method declared as public can be accessed from any other class in the same package or from any other package. It forms part of the class's public interface and can be used by external code.
// MyClass.java
package com.example;

public class MyClass {
    public void publicMethod() {
        // Method implementation
    }
}
public variable: A variable declared as public can be accessed from any other class in the same package or from any other package. It is not recommended to expose variables directly as public; instead, it's better to use getter and setter methods for encapsulation.
// MyClass.java
package com.example;

public class MyClass {
    public int publicVar; // Not recommended for direct public access
}
public Constructor: A constructor declared as public can be used to create objects from any other class in the same package or from any other package. It is useful for creating instances of a class externally.
// MyClass.java
package com.example;

public class MyClass {
    public MyClass() {
        // Constructor implementation
    }
}
private: In Java, the private access modifier is the most restrictive one. When applied to a class, method, variable, or constructor, it limits the visibility of that component to only within the same class.

private class:A class declared as private cannot be accessed from any other class, including classes within the same package. Private classes are typically used for inner classes or helper classes that are tightly coupled with the enclosing class and not meant for external use.
public class OuterClass {
    private class InnerClass {
        // Inner class implementation
    }
}
private method: A method declared as private can only be accessed from within the same class. It is used to hide certain implementation details from external classes and restrict access to the method to the class itself.
public class MyClass {
    private void privateMethod() {
        // Method implementation
    }
}
private variable: A variable declared as private can only be accessed from within the same class. It is used to hide data and restrict access to the variable to methods within the class, providing better control over the state of the object.
public class MyClass {
    private int privateVar; // Encapsulated private variable

    // Getter method to access the private variable
    public int getPrivateVar() {
        return privateVar;
    }

    // Setter method to modify the private variable
    public void setPrivateVar(int newValue) {
        privateVar = newValue;
    }
}
private constructor : A constructor declared as private cannot be used to create objects directly from any other class, not even from classes in the same package. Private constructors are typically used for implementing design patterns like Singleton, where the class should have only one instance.
public class MyClass {
    private MyClass() {
        // Constructor implementation
    }
}
protected: In Java, the protected access modifier provides a level of visibility that is more permissive than private but more restrictive than public or default (package-private) access. When applied to a class, method, variable, or constructor, it grants access to members within the same package and subclasses, whether they are in the same package or a different one.

protected class: A class declared as protected can be accessed from subclasses (regardless of whether they are in the same package or a different package) and from other classes within the same package.
// MyClass.java (in package com.example)
package com.example;

protected class MyClass {
    // Class implementation
}
protected method: A method declared as protected can be accessed from subclasses (regardless of whether they are in the same package or a different package) and from other classes within the same package.
// MyClass.java
package com.example;

public class MyClass {
    protected void protectedMethod() {
        // Method implementation
    }
}
protected variable: A variable declared as protected can be accessed from subclasses (regardless of whether they are in the same package or a different package) and from other classes within the same package.
 // MyClass.java
package com.example;

public class MyClass {
    protected int protectedVar; // Encapsulated protected variable
}
protected constructor: A constructor declared as protected can be used to create objects from subclasses (regardless of whether they are in the same package or a different package) and from other classes within the same package.
// MyClass.java
package com.example;

public class MyClass {
    protected MyClass() {
        // Constructor implementation
    }
}
default: In Java, the "default" access modifier, also known as "package-private," is used when no explicit access modifier is specified for a class, method, variable, or constructor. It provides access within the same package, meaning that the component can be accessed by any other class in the same package but not from classes in different packages.

default class: If no access modifier is specified when defining a class, it is considered to have default access. A default class can be accessed by other classes within the same package but not from classes in different packages.
// MyClass.java (in package com.example)
package com.example;

class MyClass {
    // Class implementation with default access
}
default method: If a method is defined without an access modifier, it has default access. A default method can be accessed by other classes within the same package but not from classes in different packages.
// MyClass.java
package com.example;

public class MyClass {
    void defaultMethod() {
        // Method implementation with default access
    }
}
default variable: When a variable is declared without any access modifier, it is considered to have default access. A default variable can be accessed by other classes within the same package but not from classes in different packages.
// MyClass.java
package com.example;

public class MyClass {
    int defaultVar; // Encapsulated default variable
}
default constructor: If a constructor is defined without any access modifier, it has default access. A default constructor can be used to create objects from other classes within the same package but not from classes in different packages.
// MyClass.java
package com.example;

public class MyClass {
    MyClass() {
        // Constructor implementation with default access
    }
}
summary of the accessibility of the access modifiers:

public: Allows access from anywhere, both within the class, within the package, and from any other package, even if it is a subclass or non-subclass.

protected: Permits access within the class, within the package, and from any other package. However, access is denied from outside the package if it is a non-subclass.

default: Allows access within the class and within the package. It restricts access from outside the package, whether it is a subclass or non-subclass.

private: Only allows access within the class. It denies access from any other class, regardless of whether it is within the same package or not.



Next Post Previous Post