Friday, January 10, 2025
HomeProgrammingWhat is the Difference between Public, Protected, Package Private, and Private in...

What is the Difference between Public, Protected, Package Private, and Private in Java

Summary of Differences

Modifier Same Class Same Package Subclass (Different Package) Other Packages
public Yes Yes Yes Yes
protected Yes Yes Yes No
package-private (default) Yes Yes No No
private Yes No No No

 

In Java, access modifiers are used to control the visibility of classes, methods, and variables. The key access modifiers are:

  • public
  • protected
  • package-private (default)
  • private

Here is a breakdown of each access modifier:

1. public

  • Visibility: The public access modifier allows the member (class, method, or variable) to be accessible from any other class in any package.
  • Usage: It is the least restrictive access modifier.

Example:

public class MyClass {
    public int myVariable;
    
    public void myMethod() {
        // method implementation
    }
}
  • Access: Any class, anywhere in the project, can access MyClass and its public members (myVariable, myMethod).
See also  How to send an email with Python?

2. protected

  • Visibility: The protected access modifier allows the member to be accessible:
    • Within the same package (like package-private).
    • To subclasses (even if they are in different packages).
  • Usage: It’s less restrictive than private, but more restrictive than public.

Example:

public class MyClass {
    protected int myVariable;
    
    protected void myMethod() {
        // method implementation
    }
}
  • Access: myVariable and myMethod can be accessed by any class in the same package or any subclass (even in a different package).

3. package-private (default)

  • Visibility: If no access modifier is specified, it is considered package-private (default). This means the member is accessible only within the same package.
  • Usage: This is the default level of access when no modifier is provided. It is more restrictive than public and protected but less restrictive than private.

Example:

class MyClass {  // No access modifier, so it's package-private
    int myVariable;
    
    void myMethod() {
        // method implementation
    }
}
  • Access: myVariable and myMethod can be accessed only by other classes in the same package. They are not accessible from classes in other packages.
See also  Compiling a C Program: Behind the Scenes

4. private

  • Visibility: The private access modifier is the most restrictive. It allows the member to be accessible only within the same class.
  • Usage: It is typically used for encapsulating and hiding internal details from other classes.

Example:

public class MyClass {
    private int myVariable;
    
    private void myMethod() {
        // method implementation
    }
}
  • Access: myVariable and myMethod can only be accessed from within the MyClass class. They are not accessible from any other class, even subclasses.
See also  Getting random numbers in Java

When to Use Each Modifier:

  • public: Use when you want a class, method, or variable to be accessible from anywhere (across packages).
  • protected: Use when you want a class member to be accessible within the same package and by subclasses (even if they are in different packages).
  • package-private (default): Use when you want to limit access to only classes within the same package (useful for internal implementation details).
  • private: Use when you want to hide a class member from the outside world, ensuring it can only be accessed within the same class (encapsulation).
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x