Header Ads

Package

 



Packages:

Code reusability is an important feature of an object oriented programming language. Java is purely an object oriented programming language.

Purpose:

In Java, packages are used to achieve the code reusability. That means, the classes from other programs can be easily used by a class without physically copying it to current location.

Reusability can be achieved by extending classes and implementing interfaces. But, this provides limited reusability as we can reuse the classes in the same program. If we need to reuse classes in the other parts of programs without physically copying them into the program, then only the possible solution is Packages.

There may be situations when two or more programmer wants use the same name for their classes. This will result in chaos. To achieve these objectives, Java provides feature called Package.

Definition: Package is a mechanism in which variety of classes and interfaces can be grouped together.

Advantages of Packages

Following are the benefits of organizing classes into packages

1. The classes defined in the packages of another program can be easily reused.
2. Two classes from two different packages can have the same name. By using the package name the particular class can be referred. Thus Package removes naming collision
3. Packages provide the complete separation between the two phases - Design and coding. In the design phase, we can design the classes and decide their relationship and then during the coding phase we can develop the Java code for corresponding classes and can group them in several packages according to their relationship with each other.
4. Using packages, it is possible to hide the classes. This feature prevents other programs to access the classes that are developed for internal purpose only
5. Package is used to categorize the classes and interfaces so that they can be easily maintained
6. Application development time is less, because reuse the code
7. Application memory space is less (main memory)
8. Application execution time is less
9. Application performance is enhanced (improve)
10. Redundancy (repetition) of code is minimized
11. Package provides access protection.

Type of package

Package are classified into two type which are given below.

1. Predefined or built-in package
2. User defined package

Predefined or built-in package

These are the package which are already designed by the Sun Microsystem and supply as a part of java APIevery predefined package is collection of predefined classes, interfaces and sub-package.

User defined package

If any package is design by the user is known as user defined package. User defined package are those which are developed by java programmer and supply as a part of their project to deal with common requirement

Access Protection in Packages

access protection in packages

How to Create Package

1) Create a directory which has same name as the package.
2) Include package command along with the package name as the first statement in the program.

                 Package               packagename;
     
    Where package is a keyword and packagename is userdefined.
3) Write class definition.
4) Save the file in this directory as NameOfClass.java where where .NameOfClass is the name of the class.
5) Compile this file using javac.

The CLASSPATH variable points to the directories in which the classes that are available to import. CLASSPATH variable allows you to put your own class files in various directories and lets the JDK tools know where they are.

Built in Packages

Java API is a collection of various packages in which several classes and methods are grouped together according to their functionality. 

The purpose of java system packages is as given below -

java system packages

java.lang Package

Purpose: This package provides core classes and interfaces used in design of Java language..

Interfaces

java.lang Interfaces

Classes: 

java.lang Classes


No comments

Powered by Blogger.