Saturday, March 25, 2006

Session 3- Access Modifiers

Access Modifiers:

Access Modifiers are key-words which help you to define the level of access to a particular variable or method.

The basic modifiers are:
private
protected
No Access Specified
public

private variable/method means the highest level of restriction.
The private entity cannot be accessed by any other Class apart from the Class in which it was created
Its like a man's shaving kit.


protected variable/method is the next level in restricting access.
The protected entity cannot be accessed by any other Class apart from the Class in which it was created and the Class which has inherited it.
Lets put a note on inheritance here. By inheriting a Parent Class the Child Class gets all the properties and methods which are above the level of private restriction.
Its like the child inheriting parents genes.

No access specifier is mentioned for a variable/method means package level restriction.
If no access specifier is mentioned for a entity it is accessible by any Class with in the same package.
Its like a friend having access to you.

public variable/method is the last level in access.
The public entity is accessible to all Classes across all the system.

Scope Modifiers:
Final
Synchronized
Transient
Volatile

Final modifier means it is the end of hierarchy in the inheritance tree.
The final entity[Class,method/variable].
A final class cannot be inherited.
All variables in a final Class are final variables.
To define a Constant in java you have to specify final as the key-word.
A final method cannot be inherited-so cannot be overridden.

Synchronized modifier is used while doing multi threaded code.
The synchronized entity will not allow more than one thread to possess it at a time.

Transient modifier is only applicable for variables.
The transient variable will not be written when a class is serialized.
Serialization will be covered later.

Volatile modifier is only applicable for variables.
The volatile variable is asynchronous for value assignments.

Transient and Volatile are rarely used.


Next session is an extension of Access Modifiers, variables and data types...