Java Modifiers
Access Modifier | Same Class | Same Package Subclass | Same Package non-Subclass | Different Package Subclass | Different Package non-Subclass |
---|---|---|---|---|---|
Default | Yes | Yes | Yes | No | No |
Private | Yes | No | No | No | No |
Protected | Yes | Yes | Yes | Yes | No |
Public | Yes | Yes | Yes | Yes | Yes |
Modifiers divided into two groups:
- Access Modifiers – controls the access level
- public : The class is accessible by any other class
- private : The code is only accessible within the declared class
- protected : The code is accessible in the same package and subclasses.
- default : The class is only accessible by classes in the same package. This is used when you don’t specify a modifier.
- Non-Access Modifiers – do not control access level, but provides other functionality
- final — For classes
- static : Attributes( class variable/field member ) and methods belongs to the class, rather than an object.
- abstract — (For classes ) 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 :
- synchronized :
- volatile :