|
In all the examples which we have taken to make you understand the concepts of the various features of java we have everywhere defined the class from the same name space and it means that very time we had to give a unique name to each and every class to avoid the confusions. But this is not a very convenient practise to be followed and thus Java provides us with a concept of “package”.
We can define the classes inside a package which are accessed only by the code present in that package and hence any outside interference is strictly restricted within the package. Not only this as per your choice you can also define class members which will be accessible to other members of the same package too.
It is quite easy to define a package by using the command “package”. In the first line of any Java file or program. And any classes defined within this package will belong to that above mentioned package only.
General syntax of defining package in any java file:
package name;
Where package = key word or command
name = name given to the package
Example:
package mynewpackage;
Now all the .class files used in this package will be stored in the directory which is been created for mynewpackage package.
Packages act as containers for the various classes which are being used in that particular package and the classes act as containers for the data and the code written in that java files which are kept in the package container so we can sum up the above written statement as class is a storehouse for the data and the code and the package acts as a storehouse for the classes.
The class is basically the smallest unit of abstraction .Java provides four categories of visibility for the class members:
■ Subclasses in the same package
■ Non-subclasses in the same package
■ Subclasses in different packages
■ Classes that are neither in the same package nor subclasses
Along with this Java provides us with other three access specifiers, private, public, and protected, which provides us a variety of ways to produce the many levels of access required by these categories.
Although the access control mechanism provide by Java may look bit complicated, we can mention it in the summary form to make you understand it in a simple way:
1. Anything declared public can be accessed from anywhere.
public void display() {
System.out.println("Publically available to all the classes in all the packages.");
}
2. Anything declared private cannot be seen outside of its class.
private void display() {
System.out.println("Privately available to only class in which it is declared.");
}
3. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access. No need to mention anything in the access modifier section.
void display() {
System.out.println("Available only within the package.");
}
4. If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected.
private void display() {
System.out.println("Available only with the package and subclasses" +
" declared in the other packages.");
}
Packages provide us with a very efficient mechanism for making various compartments for various different classes and can keep them separate from each other and all the standard classes provided in Java are stored in some named package. Since we now properly understand that all the package names must always be in synchronisation with the classes stored in those packages to avoid collision and confusion but it may lead to hectic work by giving long dot separated package names and then using them everywhere when required.
To make this again simple and easier for the Java programmers, Java has given the mechanism of importing the packages. We can include the import statement to bring certain classes or the entire package into visibility. We can directly refer to the class by just writing its name if we have imported the package once.
In a Java source file, an import statement is mentioned exactly next to the package statement in the Java file, before any class definition.
General syntax for import is:
import package name 1.[package name 2].(classname|*);
Where import = keyword or command
package name 1= name of top-level first package to be imported
package name 2= name of subordinate package inside the outer package
Class name/*= name of classes to be imported from the particular package/all the class files if * is mentioned from the package.
It is recommend to define the class file names instead of mentioning * which means that you want to import all the files as it may increase the import time a lot as there are chances of having many big files in the package but it doesn’t effect the run time performance of the classes.
Java includes all its Java classes in a particular package termed as “Java”. The basic language functions are stored in a package inside of the java package called java.lang.
If by chance we have the class with similar name in two packages which we are trying to import it will be done and we will not find any error at the time of importing the class files from the packages but we will get a compile-time error and then will have to explicitly name the class specifying its package.
|