Unit VI : Packages and Applets 6.1 Introduction
We have repeatedly stated that one of the main features of OOP is its ability to reuse the code already created. One way of achieving this is by extending the classes and implementing the interfaces. This is limited to reusing the classes within a program.
What if we need to use classes from other programs without physically copying them into the program under development? This can be accomplished in Java by using what is known as packages, a concept similar to "class libraries" in other languages. Another way of achieving the reusability in Java, therefore, is to use packages.
Packages arc Java's way of grouping a variety of classes and/or interfaces together. The grouping is usually done according to functionality. In fact, packages act as "containers" for classes. By organizing our classes into packages we achieve the following benefits:
1. The classes contained in the packages of other programs can be easily reused.
2. In packages, classes can be unique compared with classes in other packages. That is. two classes in two different packages can have the same name. They may be referred by their fully qualified namc, comprising the package name and the class name.
3. Packages provide a way to "hide" classes thus preventing other programs or packages from accessing classes that are meant for internal use only.
4. Packages also provide a way for separating "design" from "coding". First we can design classes and decide their relationships. and then we can implement the Java code needed for the methods. It is possible to change the implementation of any method without affecting the rest of the design.
For most applications, we will need to use to different sets of classes, one for the internal representation of our program's data, and the other for external presentation purposes. We may have to build our own classes for handling our data and use existing class libraries for designing user interfaces. Java packages are therefore classified into two types. The first category is known as Java API packages and the second is known as user defined packages.
6.2 Java API Packages
Package name Contents java.lang
Language support classes. These are classes that Java compiler itself uses and therefore they are automatically imported They include classes for primitive types, strings, math functions, threads and exceptions.
java.util
Language utility classes such as vectors, hash tables, random numbers. date. etc. java.io
Input/output support classes. They provide facilities for the input and output of data. Java. awt
Set of classes for implementing graphical user interface. They include classes for windows, buttons, lists, menus and so on.
java. net
Classes for networking. They include classes for communicating with local computers as well as with intemet savers.
java.applet
Classes for creating and implementing applets. 6.3 Using System Packages
There are two ways 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 and 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 package, then we may do so as follows:
java.awt.Colour
Notice that awt is a package within the package Java and the hierarchy is represented by separating the levels with dots. This approach is perhaps the best and easiest one if we need to access the class only once or when we need not have to access any other classes of the package.
But, in many situations, we might want to use a class in a number of places in the program or we may like to use many of the classes contained in a package. We may achieve this easily as follows:
import packagename.Classname; Or
import packagename.*;
These arc known as import statements and must appear at the top of the file, before any class declarations, import is a keyword.
The first statement allows the specified class in the specified package to be imported. For example. the statement
imports the class Colour and therefore the class name can now be directly used in the program. There is no need to use the package name to qualify the class.
The second statement imports every class contained in the specified package. For example, the statement
import java.awt.*; will bring all classes of java.awt package.
6.4 Naming Conventions
Packages can be named using the standard Java naming rules. By convention, however, packages begin with lowercase letters. This makes it easy for users to distinguish package names from class names when looking at an explicit reference to a class. We know that all class names, again by convention. begin with an uppercase letter. For example, look at the following statement:
This statement uses a fully qualified class name Math to invoke the method sqrt( ). Note that methods begin with lowercase letters.
Every package name must be unique to make the best use of packages. Duplicate names will cause run-time errors. Since multiple users work on Internet, duplicate package names are unavoidable. Java designers have recognised this problem and therefore suggested a package naming convention that ensures uniqueness. This suggests the use of domain names as prefix to the preferred package names. For example:
cbe.psg.mypackage
Here cbe denotes city name and psg denotes organisation name. Remember that we can create a hierarchy of packages within packages by separating levels with dots.
6.5 Creating Packages
package firstPackage; / / package declaration public class FirstClass // class definition {
(body of class) }
Here the package name is firstPackage. The class FirstClass is now considered a part of this package. This listing would be saved as a file called FirstClasslava, and located in a directory named firstPackage. When the source file is compiled, Java will create a .class file and store it in the same directory.
Remember that the .class files must be located in a directory that has the same name as the package, and this directory should be a subdirectory of the directory where classes that will import the package arc located.
To recap, creating our own package involves the following steps: I. Declare the package at the beginning of a file using the form
package packagename;
2. Define the class that is to be put in the package and declare it public.
3. Create a subdirectory under the directory where the main source files are stored. 4. Store the listing as the classname java file in the subdirectory created.
5. Compile the file. This creates .class file in the subdirectory.
Remember that case is significant and therefore the subdirectory name must match the package name exactly.
As pointed out earlier. Java also supports the concept of package hierarchy. This is done by specifying multiple names in a package statement, separated by dots. Example:
package firstPackage.secondPackage.
This approach allows us to group related classes into a package and then group related packages into a larger package. Remember to store this package in a subdirectory named firstPackage/ secondPackage.
name. When a source file with more than one class definition is compiled, Java creates independent .class files for those classes.
6.6 Accessing a Package
We use the import statement when there are many references to a particular package or the package name is too long and unwieldy.
The same approaches can be used to access the user-defined packages as well. The import statement can be used to search a list of packages for a particular class. The general form of import statement for searching a class is as follows:
import packagel [.package2] [.packagn] .classname;
Here package1 is the name of the top level package, package2 is the name of the package that is inside the packagel. and so on. We can have any number of packages in a package hierarchy. Finally, the explicit classname is specified.
Note that the statement must end with a semicolon (;). The import statement should appear before any class definitions in a source file. Multiple import statements are allowed. The following is an example of importing a particular class:
import firstPackage.secondPackage.MyClass;
After defining this statement, all the members of the class MyClass can be directly accessed using the class name or its objects (as the case may be) directly without using the package name.
We can also use another approach as follows:
import packagename.*;
Here, packagename may denote a single package or a hierarchy of packages as mentioned earlier. The star (•) indicates that the compiler should search this entire package hierarchy when it encounters a class name. This implies that we can access all classes contained in the above package directly.
The major drawback of the shortcut approach is that it is difficult to determine from which package a particular member came. This is particularly true when a large number of packages are imported. But the advantage is that we need not have to use long package names repeatedly in the program.
Let us now consider some simple programs that will use classes from other packages. The listing below shows a package named package' containing a single class ClassA.
package packagel; public class ClassA {
public void displayA( ) {
System.out.println("Class A"): }
}
This source file should be named ClassA.java and stored in the subdirectory packagel as stated earlier. Now compile this java file. The resultant ClassA.class will be stored in the same subdirectory. Now consider the listing shown below:
import packagel.ClassA; class PackageTestl {
public static void main(String args[ ] ) {
ClassA objectA = new ClassA( ); objectA.displayA( );
} }
During the compilation of PackageTest1.java the compiler checks for the file ClassA.class in the packagel directory for information it needs, but it does not actually include the code from ClassA.class in the file PackageTestl.class. When the PackageTestl program is run, Java looks for the file PackageTestl.class and loads it using something called class loader. Now the interpreter knows that it also needs the code in the file ClassA.class and loads it as well. 6.8 Adding a class to a package
It is simple to add a class to an existing package. Consider the following package: package pl;
public ClassA {
// body of A }
The package p1 contains one public class by name A. Suppose we want to add another class B to this package. This can be done as follows:
1. Define the class and make it public. 2. Place the package statement
package pl; before the class definition as follows:
package p1; public class B {
// body of B }
3. Store this as Nava file under the directory pl.
4. Compile B.java file. This will create a B.class file and place it in the directory p1.
import p 1 .*;
will import both of them. Remember that, since a Java source file can have only one class declared as public, we cannot put two or more public classes together in a Java file. This is because of the restriction that the file name should be same as the name of the public class with Java extension.
6.9 Introduction to Applets
Applets are small Java programs that are primarily used in Internet computing. They can be transported over the Internet from one computer to another and run using the Applet Viewer or any Web browser that supports Java. An applet, like any application program, can do many things for us. It can perform arithmetic operations. display graphics, play sounds, accept user input, create animation, and play interactive games.
Java has revolutionized the way the Internet users retrieve and use documents on the world wide network. Java has enabled them to create and use fully interactive multimedia Web documents. A web page can now contain not only a simple text or a static image but also a Java applet which, when run, can produce graphics, sounds and moving images. Java applets therefore have begun to make a significant impact on the World Wide Web.
Local and Remote Applets
We can embed applets into Web pages in two ways. One, we can write our own applets and embed them into Web pages. Second, we can download an applet from a remote computer system and then embed it into a Web page.
An applet developed locally and stored in a local system is known as a local apple. When a Web page is trying to find a local applet, it does not need to use the Internet and therefore the local system does not require the Internet connection. It simply searches the directories in the local system and locates and loads the specified applet (see Fig. 14.1).
A remote applet is that which is developed by someone else and stored on a remote computer connected to the Internet. If our system is connected to the Internet, we can download the remote applet onto our system via at the Internet and run it (see Fig. 14.2).
CODESASE = http : // wor.netserve.com / applets
In the case of local applets, CODEBASE may be absent or may specify a local directory. In this chapter we shall discuss how applets are created, how they are located in the Web documents and how they are loaded and run in the local computer.
6.10 How Applets Differ from Applications
Although both the applets and stand-alone applications are Java programs, there are significant differences between them. Applets arc not full-featured application programs. They are usually written to accomplish a small task or a component of a task. Since they are usually designed for use on the Internet, they impose certain limitations and restrictions in their design.
• Afflicts do not use the main( ) method for initiating the execution of the code. Applets, when loaded, automatically call certain methods of Amid class to start and execute the applet code.
• Unlike stand-alone applications, applets cannot be run independently. They are run from inside a Web page using a special feature known as HTML tag.
• Applets cannot run any program from the local computer.
• Applets are restricted from using libraries from other languages such as C or C++. 6.11 Preparing to Write applets
Until now, we have been creating simple Java application programs with a single main( ) method that created objects, set instance variables and ran methods. Here, we will be creating applets exclusively and therefore we will need to know
• When to use applets, • How an applets works,
• What sort of features an applet has, and
• Where to start when we first create our own applets.
First of all, let us consider the situations when we might need to use applets.
1. When we need something dynamic to be included in the display of a Web page. For example, an applet that displays daily sensitivity index would be useful on a page that lists share prices of various companies or an applet that displays a bar chart would add value to a page that contains data tables.
2. When we require some "flash" outputs. For example, applets that produce sounds, animations or some special effects would be useful when displaying certain pages.
3. When we want to create a program and make it available on the Internet for us by others on their computers.
Before we try to write applets, we must make sure that Java is installed properly and also ensure that either the Java appletviewer or a Java-enabled browser is available.
The steps involved in developing and testing in applet are: 1. Building an applet code (Java file)
2. Creating an executable applet (.class file) 3. Designing a Web page using HTML tags 4. Preparing <APPLET> tag
7. Testing the applet code
Each of these steps is discussed in the following sections. 6.12 Building Applet Code
It is essential that our applet code uses the services of two classes, namely. Applet and Graphics from the Java class library. The Applet class which is contained in the java.applet package provides life and behaviour to the applet through its methods such as hilt( ), start( ) and point( ). Unlike the applications, where Java calls the main( ) method directly to initiate the execution of the program. when an applet is loaded, Java automatically calls a series of Applet class methods for starting. running, and stopping the applet code. The Applet class therefore maintains the lifecycle of an applet.
The paint( ) method of the Applet class, when it is called, actually displays the result of the applet code on the screen. The output may be text, graphics, or sound. The paint( ) method, which requires a Graphics object as an argument, is defined as follows:
public void paint (Graphics g)
This requires that the applet code imports the Java.awt package that contains the Graphics class. All output operations of an applet are performed using the methods defined 'n the Graphics class. It is thus clear from the above discussions that an applet code will have a general format as shown below:
import java.awt.*; import java.applet.*;
public class appletclassname extends Applet {
public void paint (Graphics g) {
/ / Applet operations code }
}
execute the code. Program 14.1 shows a simple Rennina applet. Program 14.1 The HelloJave applet