keywords that control the visibility and accessibility of classes, methods, constructors and variables. They are a core part of encapsulation, allowing you to hide sensitive data and restrict how other parts of a program interact with your code.
The access modifier in Java specifies the accessibility or scope of a field method constructors or class.We can change the access level of fields constructors methods and class by applying the access
modifier on it.
They facilitate data encapsulation by restricting which parts of a program can interact with specific class members.
Access modifiers are like keys and locks for your application.
use to change the properties of classes, methods, and variables. They define the accessibility, behavior, and scope of these components.
In Java, modifiers define the scope, behavior, and properties of classes, methods, and variables.
precedes all other keywords when creating a class, method or even a variable.
define how the members of a class, like variables, methods, and even the class itself, can be accessed from other parts of our program.
We divide modifiers into two groups:
Access Modifiers - controls the access level
Non-Access Modifiers - do not control access level, but provides other functionality
Types of Access Modifiers
Java has 4 access modifiers:
private
default
protected
public
we can see the use of both public and private. These two access modifiers in particular are very popular
While a public is the most open in access, private is the exact opposite and only exists in the class that it was initialized in or created.
Access modifiers control the visibility of classes, methods, and variables. The four access modifiers in Java are:
1.Public:
- The access level of a public modifier is everywhere.
- It can be accessed from within the class, outside the class, within the package and outside the package.
- The member is accessible from any other class.
- It has the widest scope among all other modifiers
- The least restrictive modifier.
- Top-level classes can be declared public.
2.Protected:
- The access level and members of a protected modifier is within the package and outside the package through the child class(inheritance)(subclass) located in different packages. .
- If you do not make the child class, it cannot be accessed from outside the package.
- It cannot be applied to top-level classes.
- The protected access modifier can be applied on the data members method and constructors .It can't be applied on the class
- It provides more accessibility than the default modifier
3.Default:
- The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
- default (no keyword) It provides more accessibility than privates But it is more restrictive than protected and public
- default (Package-Private): Applied automatically if you do not specify any keyword. Visibility is restricted entirely to classes within the same package.
4.Private:
The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
The most restrictive modifier. Members are visible only within the class where they are defined. Top-level classes cannot be private
The member is accessible only within its own class.A variable that controls the width and height of a window can safely be reserved for private access.
Key Rules and Restrictions
1.Method Overriding:
Method Overriding: When overriding a method, you cannot reduce its visibility (e.g., you cannot override a public method with a protected one), but you can increase it.
When overriding methods in a subclass, Java prohibits making the inherited method more restrictive. It must maintain or expand the parent method's visibility:
A protected method can be overridden as protected or public.
A default method can be overridden as default, protected, or public.
A private method cannot be overridden because it is not visible to the subclass.
2.Top-Level Classes:
A top-level class can only be public or default. It cannot be declared private or protected.
public :The class is accessible by any other class
default :The class is only accessible by classes in the same package. This is used when you don't specify a modifier.
3.For attributes, methods and constructors
public: The code is accessible for all classes
private :The code is only accessible within the declared class
default :The code is only accessible in the same package. This is used when you don't specify a modifier.
protected :The code is accessible in the same package and subclass.
4.Local Variables:
Access modifiers cannot be applied to local variables inside methods; doing so will result in a compilation error.
5.Interfaces:
By default, all methods in an interface are public and all fields are public static final
When to Use Each Access Modifier in Real-World Projects
Private: The idea should be use as restrictive access as possible, so private should be used as much as possible.
Default (Package-Private): Often used in package-scoped utilities or helper classes.
Protected: Commonly used in inheritance-based designs like framework extensions.
Public: This is used for API endpoints, service classes, or utility methods shared across different parts of an application.
Non-Access Modifiers List
Non-access modifiers do not control visibility (like public or private), but instead add other features to classes, methods, and attributes.
The most commonly used non-access modifiers are final, static, and abstract
For classes, you can use either final or abstract:
final :The class cannot be inherited by other classes .
abstract :The class cannot be used to create objects (To access an abstract class, it must be inherited from another class)
For attributes and methods, you can use the one of the following:
final :Attributes and methods cannot be overridden/modified
static :Attributes and methods belong to the class, not to objects. This means all objects share the same static attribute, and static methods can be called without creating objects.
abstract :Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from).
transient :Attributes and methods are skipped when serializing the object containing them
synchronized :Methods can only be accessed by one thread at a time
volatile :The value of an attribute is not cached thread-locally, and is always read from the "main memory"
Final:
If you don't want the ability to override existing attribute values, declare attributes as final.
Static:
A static method belongs to the class, not to any specific object. This means you can call it without creating an object of the class.
but it cannot use variables or methods that belong to an object.
Abstract:
An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass
Non-Access Modifiers
Non-access modifiers provide additional functionalities such as controlling inheritance, ensuring immutability, and defining special properties. Some common non-access modifiers are:
static: Indicates that the member belongs to the class, rather than instances of the class.
final: Used to declare constants, prevent method overriding, and inheritance.
abstract: Used to declare abstract classes and methods.
synchronized: Used in multithreading to ensure that a method or block is accessed by only one thread at a time.
volatile: Ensures that the value of a variable is always read from the main memory.
transient: Prevents serialization of class fields.
Example of Non-Access Modifiers
public class NonAccessExample {
public static final int CONSTANT = 100;
public static void main(String[] args) {
System.out.println("Constant Value: " + CONSTANT);
}
}
In this example, CONSTANT is a static and final variable, meaning it belongs to the class and cannot be changed.
Tips and Best Practices
Use Access Modifiers Wisely: Restrict access as much as possible to protect your data. Use private for fields unless they need to be accessed outside the class.
Static Usage: Use static for utility methods and constants that do not require object state.
Final for Constants: Declare constants using final and static to prevent modification.
Abstract Classes: Use abstract classes as base classes that should not be instantiated directly.
Synchronized for Thread Safety: Use synchronized to ensure thread safety when necessary, but be aware of potential performance impacts.
Volatile for Visibility: Use volatile for variables shared between threads to ensure visibility of changes.
Transient for Serialization: Use transient to exclude fields from serialization when they should not be part of the serialized object.
Top comments (0)