Ticker

6/recent/ticker-posts

JAVA - Access specifiers | Access modifiers

Accesses specifiers 

  • Public 
  • Protected 
  • Default 
  • Private 

The accesses specifiers will define the scope of every member 

In java accessibility of any member can be controlled by using accesses modifiers. In picture the accessibility can be given has 

PUBLIC is the high visible access specifier in java which can be accessed every were 

PRIVATE is the highly restricted access specifier in java that it can be accessed form only inside the class. Such that the members which are declared has private cannot be accessed from outside the class 

DEFAULT the scope of default members in only inside the package. They can be accessed within package but not from outside the package. If we don’t specify any access specifier than the member would be taken has default access specifier 

PROTECTED the protected members can be accessed within the package, but they can be accessed outside the package also only in the case of inheritance

 Access modifiers 

We know access specifiers specify the access and access modifiers modify the access of variables, methods and classes. In general, an access modifier works between the classes of the same application or within the classes of same package. 

Six modifiers exist that can be used with methods. 

  • 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 operation 
  • strictfp Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed 

Four modifiers and four specifiers exist that can be applied to variables 

  • 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 

Three modifiers exist that can be applied to classes. Modifier comes just before the class name 

  • 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 

Points to note in method overriding with respect to access specifiers 

  • If super class method is public, while overriding it in child class, it should be public, Since it is the weakest access specifier or least restrictive access specifier 
  • If super class method is protected, while overriding it in child class, it can be public or protected but not anything else 
  • If superclass method is default, then while overriding it in child class, it can be public, protected or no access, but cannot be private. 
  • If superclass method is private, then while overriding it in child class, it can be anything. Since private method cannot be overrided.

ALSO SEARCH:

"access modifiers in java"

"access modifiers and access specifiers in java"

"default access modifiers in java"

"non access modifiers in java"

"protected access modifiers in java"

"private access specifier in java"

"public access specifier in java"

"what is the difference between access modifiers and access specifiers in java"

"what is java default access modifier"