Beruflich Dokumente
Kultur Dokumente
API Programming
1.1 Objectives
In this section, we will be discussing on what APIs are and how we could build ones
ourselves. On top of the concepts of encapsulation, inheritance, and polymorphism, we
additionally discuss the concept of packages and how it is related to the concept of API
programming.
1.2 Introduction
Prior to this chapter, we have looked on certain OO principles such as encapsulation,
inheritance, and polymorphism and how they are implemented in Java. Although they
serve a number of purpose, we focus on how they serve the purpose of API
programming, albeit simplistically.
On inheritance, this helps you in customizing an existing API, and thus build your own
API out of it. Of course, this also sets the precedent that other programmers who would
want to use your API but would want to modify it to suit their needs on specific
scenarios, would have them extend your API and create their own version for their own
needs. Again, the concept of reusability of code under object oriented programming is
once again promoted.
Introduction to Programming I 1
J.E.D.I.
1.3 Packages
Packages are Javas means of grouping related classes and interfaces together in a
single unit. This powerful feature provides for a convenient mechanism for managing a
large group of classes and interfaces while avoiding potential naming conflicts.
import <nameOfPackage>;
For example, if you want to use the class Color inside package awt, you have to type the
following,
import java.awt.Color;
import java.awt.*;
The first statement imports the specific class Color while the other imports all of the
classes in the java.awt package.
Another way to import classes from other packages is through explicit package
referencing. This is done by using the package name to declare an object of a class.
java.awt.Color color;
package <packageName>;
Suppose we want to create a package where we will place our StudentRecord class,
together with other related classes. We will call our package, schoolClasses.
The first thing you have to do is create a folder named schoolClasses. Copy all the
classes that you want to belong to this package inside this folder. After copying, add the
following code at the top of the class file. For example,
package schoolClasses;
Introduction to Programming I 2
J.E.D.I.
Packages can also be nested. In this case, the Java interpreter expects the directory
structure containing the executable classes to match the package hierarchy.
Before we discuss how to set the classpath, let us take a look at an example on what will
happen if we don't set the classpath.
Suppose we compile and then run the StudentRecord class we wrote in the last section,
C:\schoolClasses>javac StudentRecord.java
C:\schoolClasses>java StudentRecord
Exception in thread "main" java.lang.NoClassDefFoundError:
StudentRecord (wrong name: schoolClasses/StudentRecord)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown
Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native
Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown
Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown
Source)
We encounter a NoClassDefFoundError which means that Java did not know where to
look for your class. The reason for this is that your class StudentRecord now belongs to a
package named studentClasses. If we want to run our class, we jave to tell Java about
its full class name which is schoolClasses.StudentRecord. We also have to tell JVM
where to look for our packages, which in this case is in location C:\. To do this, we must
set the classpath.
where C:\ is the directory in which we have placed the packages. After setting the
classpath, we can now run our program anywhere by typing,
For Unix base systems, suppose we have our classes in the directory
/usr/local/myClasses, we write,
export classpath=/usr/local/myClasses
Take note that you can set the classpath anywhere. You can also set more than one
classpath, we just have to separate them by ;(for windows) and : (for Unix based
Introduction to Programming I 3
J.E.D.I.
set classpath=C:\myClasses;D:\;E:\MyPrograms\Java
export classpath=/usr/local/java:/usr/myClasses
Packages
Provides the classes necessary to create
java.applet an applet and the classes an applet uses
to communicate with its applet context.
Introduction to Programming I 4
J.E.D.I.
Introduction to Programming I 5
J.E.D.I.
Introduction to Programming I 6
J.E.D.I.
Introduction to Programming I 7
J.E.D.I.
javax.activation
javax.annotation
Introduction to Programming I 8
J.E.D.I.
javax.jws
javax.jws.soap
Introduction to Programming I 9
J.E.D.I.
Package javax.print.attribute.standard
javax.print.attribute.standard contains classes for specific printing
attributes.
Provides implementations of
RMIClientSocketFactory and
javax.rmi.ssl RMIServerSocketFactory over the
Secure Sockets Layer (SSL) or Transport
Layer Security (TLS) protocols.
Introduction to Programming I 10
J.E.D.I.
Introduction to Programming I 11
J.E.D.I.
Introduction to Programming I 12
J.E.D.I.
Introduction to Programming I 13
J.E.D.I.
javax.xml.stream
javax.xml.stream.events
javax.xml.stream.util
Introduction to Programming I 14
J.E.D.I.
Introduction to Programming I 15
J.E.D.I.
Introduction to Programming I 16
J.E.D.I.
org.w3c.dom.bootstrap
Introduction to Programming I 17
J.E.D.I.
org.w3c.dom.events
org.w3c.dom.ls
Introduction to Programming I 18