Access specifiers vs Access modifiers in java
Access Specifiers, Modifiers are different. Specifier specifies access and modifier modifies access.
Java supprots both Specifiers and Modifiers.
Access specifier can be applied ⇒ To any identifier variables, methods and classes, But
Access modifiers cannot be applied to every identifier, there are limitations.
Access Specifiers:
Explanation:
Public: Visible to the world.
- A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.
- However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.
Private: Visible to the class only.
- Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.
- Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
- Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.
- Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.
Protected: Visible to the package and all subclasses.
- Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
- The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
- Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
Default:
- Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
- A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
Access Modifier's:
Explanation:
For Variable:
MODIFIER KEYWORD | MEANING |
---|---|
static | Also known as "class variable". No object is necessary to call |
final | cannot be reassigned |
transient | Not serialized |
volatile | Value is more liable to change |
For Method:
MODIFIER KEYWORD | MEANING |
---|---|
final | Subclass cannot override |
abstract | No method body exists |
native | Java method takes the help of underlying OS |
static | Object is not necessary to call |
synchronized | Used to lock the source and renders a thread-safe operaton |
strictfp | Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed |
For Class:
MODIFIER KEYWORD | MEANING |
---|---|
abstract | Objects cannot be created |
final | cannot be inherited |
strictfp | Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed |
Comments
Post a Comment