This lesson deals with abstract, transient, volatile, native non access modifiers. We have already seen static and strictfp, synchronized and final non access modifiers.
The abstract Non Access Modifer
The abstract qualifier is out and out object oriented. It can be applied to
- Methods
- Classes
abstract Methods
- A method is declared abstract by saying so in the method definition and declaration.
- An abstract method is an empty method. Definition of method is delegated to sub classes. There are exactly zero statements in an abstract method. (A non abstract method can also have zero statements in it).
- An abstract method can never be final. abstract methods are intended to be overridden by sub classes.
- Any class containing an abstract method must itself be abstract.
- Any subclass of a class containing abstract method must either itself be abstract or must override the abstract method.
- All methods declared in an interface are abstract.
abstract Classes
- A class is declared abstract by using the abstract keyword in class definition.
- An abstract class may contain zero or more abstract methods. It may also contain zero or more concrete methods.
- A class having one or more abstract methods must be declared abstract.
- An abstract class cannot be instantiated.
- An abstract class cannot be final.
- An abstract class provides a template for the more specific child classes.
The native Non Access Modifer
- The native non access modifier can be applied only to methods.
- Can be applied to both static and instance methods.
- Tells the compiler and the JVM that the implementation of the method will not be found in Java. Meaning, JVM’s method area will not contain code describing this method.
- Indicates that the code has been written in an external language. All of the native code is written in C and C++.
- Involves the following steps –
- Generating the header files
- Writing the implementation of the native method in C/C++
- Using your C/C++ compiler to create a shared library
- Run the program using java interpreter
The volatile Non Access Modifer
- Applies only to instance variables. Cannot be applied to other constructs such as local variables or methods or classes.
- Tells the Java thread not to rely on the value currently visible to it.
- A volatile variable may change value in main memory while the thread continues to see the value it’s PC register. If you need a refresher, see this previous lesson – Inside JVM.
- Therefore, upon every access of a volatile variable, the runtime fetches the latest value from memory.
- Reads and writes to a volatile variables are atomic. This point will be discussed further in concurrency lessons, but know that Java data types such as long (64 bits) may map to more than one word of underlying architecture.
- By virtue of above fact, volatile variables can be used in multithreaded programs
- Declaring a member to be volatile is cheapest way of writing lock-free multithreaded code
- A volatile primitive data type such as float and char stores the value directly.
- A volatile reference data type stores the reference to an object. Therefore, the underlying object may or may not change and has got no direct bearing on declaring the reference volatile. It is far less idiomatic to write Java programs that work by continually assigning new objects to reference.
- Due to points #8 and #9, volatile variables are far more useful and common with primitive data types.
The transient Non Access Modifer
The transient modifier –
- Applies only to instance variables
- Tells the JVM to not include the field in serialization (and therefore de-serialization as well)
- Is generally used when an instance variable of an object is a function of factors outside object. For example, you may have a boolean field called isMoving inside the vehicle. Whether a vehicle is moving or not depends on the external traffic light and traffic conditions. Therefore, such variables should be declared transient.