Pages

Monday 12 August 2019

Use of modifiers

Basic use of modifiers:

Access Modifiers(public,protected,default[package access],private)
Comparison between access modifiers

Visibility
private
default
protected
public
1.Within the same class
Yes
Yes
Yes
Yes
2.From child class of the same package
No
Yes
Yes
Yes
3.From the non-child class of the same package
No
Yes
Yes
Yes
4.From child class of outside package
No
No
Yes, But you must use child class reference only
Yes
5.From the non-child class of outside package
No
No
No
Yes


Non-Access Modifiers(transient,volatile,final,abstract,native,synchronized,static,strictfp)
Impact of final keyword:
If you use keyword final with top-level class, you can't create a subclass of this class.
Example:
  1. public final class A{}
  2. public class B extends A{} // Compiler Error.
If you use the keyword final with the method, you can't override this method.
If you use the keyword final with an instance variable, you must initialize this variable before constructor completion otherwise you will get a compiler error.
Following are the various possible places for the initialization
1.At the time of declaration 
Example:
  1.  final int a =10;
2.Inside instance block
Example:
  1. final int a;
  2. {a = 10;}
3.Inside constructor
Example:
  1. class Test{
  2. final int a;
  3. Test(){
  4. a =10;
  5. }
  6. }
If you are doing initialization other than above places you will get compiler error.
Note: Once the final variable is initialized you can't change it. 
          final instance variable become compile time constant.
If you use keyword final with static variable, you must initialize this variable before class loading completion otherwise, you will get a compiler error.
The following are the various possible places for the initialization.
1.At the time of declaration
Example:
  1.  final static int a =10;
2.Inside static block
Example:
  1. final static int a;
  2. static {a = 10;}
You can use keyword final with local variables or method parameters.
Note: The final is the only modifier that is applicable for local variables and method parameters.
abstract keyword
  • You can use the keyword abstract with top-level classes and methods only.
  • You can't use the keyword abstract with variables.
  • If you use the keyword abstract with class, you can't create an object of this class.
  • If you declare an abstract method(method without body) in a class, you must make that class abstract otherwise you will get a compiler error.
static keyword
  • It is applicable for variables, methods, and blocks
  • If you use keyword static with variables, then initialization happens only once and you can share the same copy throughout the application.
  • If you use keyword static with methods, you can't override this method.
native keyword
  • It is applicable only for methods.
  • The methods which are implemented in non-java is called native method.
transient keyword
  • It is only applicable to variables.
  • If you use keyword transient with an instance variable, this variable won't be serialized.
  • If you use keyword transient with static variables, there is no impact. Because the static variable is not part of the object.
  • If you use keyword transient with the final variable, there is no impact. Because final variables are by default eligible for serialization.
volatile keyword
  • It is only applicable to variables.
  • You can solve the visibility problem by using keyword volatile in a multithreaded environment.
synchronized keyword
  • You can use the keyword synchronized with methods and blocks only.
  • You can solve the data inconsistency problem by using the keyword synchronized in a multithreaded environment.
stricfp keyword
  • You can use keyword stricfp with classes, interfaces, and methods only.
  • To ensure that calculations using floating-point numbers are identical on all platforms.