Please enable JavaScript.
Coggle requires JavaScript to display documents.
Modifiers - Coggle Diagram
Modifiers
The Java language has these modifiers:
The final modifier for finalizing the implementations of classes, methods, and variables
The abstract modifier for creating abstract classes and methods
The static modifier for creating class methods and variables
The synchronized and volatile modifiers, which are used for threads
The public, protected, and private modifiers for controlling access to a class, method, or variable
Access Control for Methods and Variables
By using access control, you can dictate how your class is used by other classes.
Some variables and methods in a class are of use only within the class itself and should be hidden from other classes. This process is called
encapsulation
:
An
object
controls what the outside world can know about it and how the outside world can interact with it
Encapsulation is the process that prevents class and instance variables from being read or modified by other classes.
The Java language provides four levels of access control
Public Access
In some cases, you might want a method or variable in a class to be completely available to any other class that wants to use it
The public modifier makes a method or variable completely available to all classes.
The main() method of an application must be public. Otherwise, it could not be called by a Java Virtual Machine (JVM) to run the class.
Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.
Protected Access
Protected variables are available to subclasses, even if they aren’t in the same package.
Private
A private variable is useful in two circumstances:
When another class could wreak havoc by changing the variable in an inappropriate way
When other classes have no reason to use that variable
To completely hide a method or variable and keep it from being used by other classes, use the private modifier.
The only place these methods or variables can be accessed is within their own class.
Neither private variables nor private methods are inherited by subclasses.
Default access
A variable or method declared without an access control modifier is available to any other class in the same package.
Any variable declared without a modifier can be read or changed by any other class in the same package.
Any method declared the same way can be called by any other class in the same package