Access Modifiers or Access Specifiers

In Java, access modifiers are keywords used to define the accessibility or visibility of classes, variables, methods, and constructors. There are four types of access modifiers in Java:

image 1
Image referred from https://www.geeksforgeeks.org/access-modifiers-java/

1. public:

The public access modifier in Java is used to declare that a class, variable, method, or constructor is accessible from anywhere in the program, both within the same package and from other packages. It provides the widest level of accessibility.

Classes: A public class can be accessed from any other class or package. It can be used as a top-level class or an inner class.

public class MyClass {
    // Class members and methods
}

Variables: A public variable can be accessed and modified directly from any class or package.

public int public_Variable;

Methods: A public method can be called from any class or package. It can be overridden by subclasses, and it can also be accessed through an instance of the class.

public void publicMethod() {
    // Method body
}

Constructors: A public constructor can be used to create objects of a class from any other class or package.

public MyClass() {
    // Constructor body
}

2. protected:

The protected access modifier in Java is used to declare that a class member (variable, method, or constructor) is accessible within its own class, subclasses (in any package), and other classes within the same package. It provides a level of accessibility more restricted than public, but wider than the default and private access modifiers.

Variables: A protected variable can be accessed within the class that declares it, as well as within subclasses (even if they are in different packages) and other classes within the same package.

protected int protected_Variable;

Methods: A protected method can be called within the class that declares it, as well as within subclasses (even if they are in different packages) and other classes within the same package. Subclasses can override the protected method.

protected void protectedMethod() {
    // Method body
}

Constructors: A protected constructor can be accessed within the class that declares it, as well as within subclasses (even if they are in different packages) and other classes within the same package. It can be used to create objects within the subclasses.

protected MyClass() {
    // Constructor body
}

3. default:

In Java, if no access modifier is explicitly specified for a class, variable, method, or constructor, it is considered to have the default access modifier. The default access modifier is useful when you want to limit the visibility of members within the package. The default access, also known as package-private, allows access only within the same package.

Classes: A class with default access can be accessed only within the same package. It is not accessible from outside the package, including subclasses in other packages.

class MyClass {
    // Class members and methods
}

Variables: A default (package-private) variable can be accessed and modified within the same package. It is not accessible from outside the package.

int default_Variable;

Methods: A default (package-private) method can be called within the same package. It is not accessible from outside the package. However, it can be overridden by subclasses within the same package.

void defaultMethod() {
    // Method body
}

Constructors: A default (package-private) constructor can be used within the same package to create objects of a class. It is not accessible from outside the package.

MyClass() {
    // Constructor body
}

4. private:

In Java, the private access modifier is the most restrictive access level. It is used to declare that a class member (variable, method, or constructor) is accessible only within the same class. It provides a high level of encapsulation and prevents direct access from other classes, including subclasses.

Variables: A private variable can only be accessed and modified within the same class. It is not accessible from any other class, including subclasses and classes in the same package.

private int private_Variable;

Methods: A private method can only be called within the same class. It is not accessible from any other class, including subclasses and classes in the same package. Private methods are typically used for internal implementation details that should not be exposed or called directly from outside the class.

private void privateMethod() {
    // Method body
}

Constructors: A private constructor can only be used within the same class to create objects. It is not accessible from any other class, including subclasses and classes in the same package. Private constructors are often used for implementing singleton classes or for restricting the direct instantiation of a class.

private MyClass() {
    // Constructor body
}

Using the private access modifier ensures that the member is hidden from the outside world and can only be accessed and modified by the class itself. It helps enforce encapsulation and protects the internal state and behavior of a class.

courtesy: https://chat.openai.com/

Leave a Reply

Your email address will not be published. Required fields are marked *