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 KEYWORDMEANING
staticAlso known as "class variable". No object is necessary to call
finalcannot be reassigned
transientNot serialized
volatileValue is more liable to change

For Method:


MODIFIER KEYWORDMEANING
finalSubclass cannot override
abstractNo method body exists
nativeJava method takes the help of underlying OS
staticObject is not necessary to call
synchronizedUsed to lock the source and renders a thread-safe operaton
strictfpGuarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed


For Class:

MODIFIER KEYWORDMEANING
abstractObjects cannot be created
finalcannot be inherited
strictfpGuarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed



Rules of Access Specifiers in Method Overriding:


Access Specifiers Method Overriding: The only rule says:
"The subclass overridden method cannot have More Restricted access than super class method".






Comments

Popular posts from this blog

FC Barcelona vs Real Madrid -El Clasico Rivalry.

1NF,2NF, 3NF Normalization

Memory Management (Contiguous and Non-Contiguous ).