Declarations and Access Modifiers


Java Source File Structure:
A java program can contain any no of classes but at most one class can be declared as the public if there is a public class the name of the program and name of public class must be matched otherwise we will get compile time error. If there is no public class then we can use any name as java source file name,There are no restrictions.

Example :


Output:
Compile time error: Cannot find Symbol.
                  Symbol: class ArrayList
                  Location:class Test

We can resolve this problem by using fully qualified name java.util
The problem with usage of fully Qualified name every time increases length of the code and reduces readability.
We can resolve the problem by using import statement.

Example:


Example:



2) default members :
If we declare a member as default then we can access that member only with in the current package & we can't access from outside of the package.
Hence, default access is also package.level access.

3) private members :
If we declare a member as private then we can access that member only wihin current class.
Abstract method should be visible in child classes to provide implementation. Where as private method are not visible in child classes. Hence private abstract combination is illegal for methods.

4) protected members (the most misunderstood modifier in java):
If we declare a member as protected then we can access that member within the current package anywhere but outside package only in child classes.
protected= default+kids of an another package (only child reference)
Within the current package we can access protected member either by parent reference or by child reference.
But from outside package we can access protected member only by using child reference. If we are trying to use parent reference we will get Compile Error

Example :



final Variables:
In general for instance & static variables it is not required to perform initialization explicitly JVM will always provide default values.
But for the local variable JVM won't provide any default values compulsory we should provide initialization before using that variable.

final instance Variables:
For the normal instance variable it is not require perform initialization explicitly Jvm will always provide default values.
If the instance variable declared as the final then compulsory we should perform initialization whether we are using or not otherwise.
We will get compile time error
Example :



Static modifier:
Static is the modifier applicable for variable & methods but not for classes (but inner class can be declared as static).
If the value of a variable is varied from object to object. Then we should go for instance variable.In the case of instance value for every object a separate copy will be created.
If the value of a variable is same for all objects.Then we should go for static variables.In the case of static variable only one copy will be created at class level & share.That copy for every object of that class.

Native modifier:
Native is the modifies applicable only for methods but not for variable and classes. The native methods are implemented in some other languages like C & C++ hence native methods also known as "foreign methods". The main objectives of native keyword are:
1)To improve performance of the system.
2)To use already existing lagecy non-java code (Pseudo Code). To use native keyword:
Example:



Disadvantage:
The main disadvantage of native keyword is it breaks platform independent nature of java because we are depending on result of platform dependemt languages.

Synchronized modifier: Synchronized is the modifier applicable for methods and blocks. We can't declare class and variable with this keyword.  If method or block declared as synchronized then at a time only one thread is allowed to operate on the given object.  Main advantage of synchronized keyword is it can resolve data inconsistency. But the main disadvantage of synchronized keyword is it increases waiting time of thread and effects performance of the system.Hence if there is no specific requirement it is never recommended to use synchronized keyword.

Transient modifier:
 Transient is the modifier applicable only for variables and we can't apply for method and classes.  At the time of serialization, if we don't want to save the value of a particular variable to meet security constraint, then we should go for transient keyword.  At the time of serialization JVM ignores the original value of transient variable and default value will be serialization.

Volatile modifier: Transient is the modifier applicable only for variables but not for methods and classes. If the value of a variable keep on changing such type of variables we have to declare with volatile modifier. If variable declared as volatile then for every thread a separate local copy will be created. Every intermediate modification performed by that thread will take place in local copy instead of master copy.  Once the value got finalized just before terminating the thread the master copy value will be updated with local state value. 
The main advantage of volatile keyword is we can resolve data inconsistency problems.  But the main disadvantage of volatile keyword is creating and maintaining a separate copy for every thread increases complexity of the programing and effects performance of the system.Hence, if there is no specific requirement it is never recommended to use volatile keyword and it is almost outdated keyword.

Volatile variable means its value keeps on changing where as final variable means value never changes. Hence final-volatile combination is illegal for variables.

Conclusions:
1) The only applicable modifier for local variable is final. The modifier which are applicable only for local variable is variable ,but not for classes and methods are volatile and transient.
2) The modifiers which are applicable only for methods but not for classes and variables native and transient.
3) The modifiers which are applicable only for method but not for classes and variables native and synchronized.
4) The modifiers which are applicable for top level classes, methods and variables are public default and final.

Interface:
 Any service requirement specification(srs) is considered as interface. From the client point of view an interface defines set of services what is expecting.  From the programmer point of view interface define the set of services what is offering.  Hence an interface considered as contract between client and service provider.

Example:
By using Bank Atm Gui Screen ,Bank people will highlight the set of services what they are offering same time.The same time screen describe the set of services what end user expected.Hence this gui screen acts as contract between client and service provider.  With in the interface we can't write any implementation,because it has to highlight just the set of services what we are offering or what you are expecting.Hence every method present inside interface should be abstract.Due to this interface is considered as 100% pure abstract class.

Declaratoin and Implementation of interface:  We can declare an interface by using Interface keyword,We can implement an interface by using implements keyword. 

Example:



If class implements an interface compulsory we should provide implementation for every methods of that interface. Otherwise we have to declare class as object.Violation leads to compile_time Error.
extends Vs implements:
1) A class can extend only one class at a time.
2) A class can implement no. of interfaces ata time.
3) A class can extend a class and implement interface simulteneously.
4) An interface can extend any no. of interfaces at a time.

concrete class Vs abstract class Vs interface:
 interface:We don't know anything about implementation just we have requirement specification,then we should go for interface. Ex. Servlet 

Abstract class:We are talking about implementation but not completely (just partially implementation) then we should go for abstract class. Ex.Generic servlet,HTTP servlet. 

Concrete class:We are talking about implementation completely and ready to provide service,then we should go for concrete class. Example our own class