Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Package Programming in Java

In this article we will talk about java programming. We will see what is Package programming in object oriented programming language.

Java is an object oriented programming language. We know that the object oriented programming provides different types of advantages. The one of the most important advantage is Encapsulation. Encapsulation is a technique that supports no outside world data access (directly) and also multiple related objects can be grouped under one object. Java programing also implements this type of process by the use of Package Programming. The Java classes which is created in the same file or each in its own file. The file that contains the class needs to maintain the protocol that file name and class name both are same.

Package programming is one of the major skills in Java. In the situation where different developer or programmer are working with their different project across in the different department as well as different city, countries then each developer take a choice to create classes which is based on the requirement. In the above consequence the project should be completed with classes with same name. Packages are classified into two types. The first category is known as java API (Application Programming Interface) packages and the second one is known as user defines packages. Java API provides a large number of classes grouped into different package according their functionality. This classification is given below.

Frequently used API packages

Figure 1: Frequently used API packages

STAND ALONE APPLICATION BASED PACKAGE

Package Name Purpose
1. java.lang This is a language support classes. These classes the java compiler itself uses therefore they are automatically imported.
2. java.util This is a language utility classes such as random numbers, date, vectors etc.
3. java.io This is Input/output support classes. This class provides the facilities for the input and output of data.

WEB BASED APPLICATION BASED PACKAGE

Package Name Purpose
1. java.awt AWT stands for abstract window toolkit. This is a Set of classes for implementing graphical user interface. They are having classes for windows, buttons, lists, menus etc.
2. java.net This Class represents networking. They are having classes for communicating with local computers as well as with internet servers.
3. java.applet This Class for creating and implementing applets.

Process to using System Package in Java Program

The packages are organized in a heretical structure. This shows that the package name java contains the package awt (Abstract Window Toolkit), which in turn contains various classes required for implementing graphical user interface.

Hierarchical representation of java.awt package

Figure 2: Hierarchical representation of java.awt package

Generally there are two process of accessing the classes stored in a package. The first approach is to use the fully qualified class name of the class that we want to use. This is done by using the package name containing the class then appending the class name to it using the dot operator. For example, if we want to refer to the class color in the awt (Abstract Window Toolkit) package, then we can write as java.awt.Color in many situations we might want to use a class in number of places in the program or we may like to use many of this class contain in a package. We can do this easily as following methods.

import packagename.classname; or import packagename.*;

These are also known as import statements and must appear at the top of the file before any class declaration.

Process to Naming Rules of Package

  • Packages always begin with lower case letters.
  • The entire class name begins with upper case letters.
Process Naming Rules

Figure 3: Process Naming Rules

  • This statement uses a fully qualified class name Math to invoke the method pow().

Concept of Package Levels

The Package names are setting with different types of levels. Each levels separate from next with some specific era.

For Example:

The java.applet package is contained of two levels:

  • The first level is java.
  • The second level is applet.

The java.awt.image package is contained of three levels:

  • The first level is java.
  • The second level is applet.
  • The third level is image.
    • Every package should be plotted to a subdirectory of the identical name in the file system.

Concept of Importing Packages

When we wish to use the object from the package program then that package should be imported into our program because without the importing the particular package we can’t access this objects.

Java uses the import statement for importing the package in our programs.

This import statement allows as well as classes and also interfaces by defining in package to be referred by their names. A (*) asterix sign [Generally it is called Meta character] is also be used to addressing all classes within a package.

There are two ways of import statement

  • import <package name><class name>;
  • import <package name>.*;

The package name is a single package name or a list which is nested package names separated by periods. The first statement that is specified for specific class and that also import only that class which is specific. But in the second statement is also access all classes which present inside importing package.

The import statement is used at the beginning of the every program file.

In that stage we consider some major advantages present in java package are following.

Purpose of the Package in Java:

  • Package are generally used to organize the classes fitting to same category or given that similar functionality.
  • A package provides a primary namespace [Namespace is collection for set of identifier].
  • The class which is belonging in same package can access each other package-access member.
  • Classes are shearing the same name only if they are present in other package.
  • A package provides a means to group related classes.
  • To create a package we need two level each file in which the classes are defined with the name of the package.
  • We can easily decide that the classes and interfaces in a single package are related.
  • In generally, the names of our classes and interfaces should not conflict with the names in other packages because the package creates a new namespace.
  • You can permit the classes within the package to have unrestricted access to one another yet still restrict access for types outside the package.
  • In general, we can also declare the package name with our common name for easily remembers.

Syntax of the Package in Java:

package <Package Name>; 

Process to create package in Java:

At first create a folder like a name <cei> and store a program.

Listing 1: Sample showing Example using package. This is the package program

armstrong.java
public class armstrong
{
	int x=407;
	int r,temp,a=0;
	public void test()
	{
	temp=x;
	while(x>0)
	{
		r=x%10;
		a=a+(r*r*r);
		x=x/10;
	}
	if(temp==a)
	{
		System.out.println("This is Armstrong no..");
	}
else
{
		System.out.println("This is not Armstrong no..");
}	
}	
} 

After the creation of the program then compile this program with following syntax:

javac  armstrong.java

If compile this part with successfully then write down this program.

Listing 2: Sample showing Example calling package

cal.java
import cei.*;
public class cal{
    public static void main(String args[]){
	   armstrong ob=new armstrong();
	   ob.test(); //this part is calling package method
    }
}

Then compile the code cal.java

javac  cal.java

After that we interpreting this program cal.java

java  cal

Then this call.java will be interpreted and run this program and show the output

Output:

This is Armstrong no.

Process to Compile package program and run.

Figure 4: Process to Compile package program and run.

Note:

  • Two points should be remembering that package name and folder name is always same.
  • Package programming also declare with public keyword for it can globally access.
  • We can also set Classpath if the package folder and calling program does not present in same directory.

Understanding Classpath

Process to display the current CLASSPATH variable; use the following commands in Windows and also in UNIX System (Bourne shell):

  • Windows System-> C:\> set CLASSPATH
  • UNIX System -> % echo $CLASSPATH

Process to delete the current contents of the CLASSPATH variable, use:

  • Windows System -> C:\> set CLASSPATH=
  • UNIX System -> % unset CLASSPATH; export CLASSPATH

Process to set the CLASSPATH variable:

  • Windows System -> set CLASSPATH=C:\users\cei\java\classes
  • UNIX System -> % CLASSPATH=/home/cei/java/classes; export CLASSPATH

In the above coding process we can set our class path as well as Windows system and also UNIX.

Access Protection of the Java package

We know that class has only two possible access levels one is called default and other is called public. In public class declaration, it is accessible by any other code. But in case of default access, then it can only be accessed by other code within its same package.

The following table gives a real picture for accessing different classes and also shows which type access is possible and which is not.

Table representation package access possibility from different classes

Figure 5: Table representation package access possibility from different classes

Conclusion

There is no doubt that java package is one of the most important part for the efficient java programmers. We can find out that java supports user defined packages as well as system defined packages. So we can take a simple conclusion, Package programming in java is not only upgrading our coding style but also reduce lot of additional job.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login