Page 1145
Page 1146
This chapter presents the predominant technologies in use on the World Wide Web, including the following:
In the relatively brief time that Java has been available, it has gained incredible popularity, and the use of Java continues to evolve ”from the development of simple applets to enterprise solutions. One of the primary reasons for Java's success is that it was designed from the ground up as a platform-independent, object-oriented language with distributed systems in mind. The following sections provide a brief overview of the Java language.
The key features of the Java object model are the class and the interface. Java classes provide encapsulated implementation, and Java interfaces provide a layer of abstraction and the means of providing true polymorphism. Polymorphism is an object-oriented term that simply means that existing code can be reused with minor behavioral modifications, thus producing a whole object without re-creating the wheel.
All Java source code (with the exception of built-in types, operators, and language syntax) exists within classes. Classes can define public, protected, and private variables and methods. Java allows constructors and class methods to be overloaded and supports static members , nested classes, and inheritance. Java also provides mechanisms you can use to control inheritance through the abstract and final keywords. Classes defined as abstract serve as pure virtual base classes; they cannot be instantiated directly. On the other end of the spectrum, classes declared as final cannot be inherited at all.
Interfaces provide a layer of abstraction by hiding class implementations . To illustrate this point, consider the following example:
public interface I {...} public class A implements I {...} public class B implements I {...} public class C { public I getAnInterface() {
Page 1147
if (some_condition) return (new A()); else return (new B()); }
The getAnInterface() method of class C returns a reference to interface I, which can be implemented by class A or class B. From the perspective of the caller, it doesn't matter which underlying implementation is used because it can invoke the methods of the interface without regard to implementation. Only abstract methods and static/final variables (constants) can be defined in an interface.
A Java class can inherit (extend) only one class, but it can implement multiple interfaces. An interface can never implement ”but it can extend ”another interface. The following declarations are legal:
public class C1 {...} public class C2 {...} public interface I1 {...} public interface I2 {...} class C3 extends C1 implements I1, I2 {...} interface I3 extends I1 {...}
Based on the previous declarations of C1, C2, I1, and I2, these declarations are illegal:
class C3 extends C1, C2 {...}: Classes cannot extend multiple classes. interface I3 implements I1 {...}: Interfaces cannot implement other interfaces. interface I4 extends I1, I2 {...}: Interfaces cannot extend multiple interfaces.
The Java package provides a further means of encapsulation by defining scope for classes and interfaces. Classes and interfaces within a package have access to all other classes and interfaces defined in the same package. However, classes and interfaces can only access public classes and interfaces and all non-private methods and variables defined in external packages. Consider the following example:
package P1; class A {...} package P1; public class B {...} package P2; class C {...}
Based on these declarations, class A is visible only to class B, class B is visible to A and C, and class C is not visible to A or B.
Methods and variables within class definitions are governed by similar scoping rules. Methods and variables defined as public are visible externally, whereas private methods and variables are visible only to members of the class. Protected members are visible to descendants and classes within the same package.
Page 1148
Class variables defined as static are shared by all instances of a class, and methods defined as static can be invoked without creating an instance of the class. The final keyword is used with static to declare a constant. The following fragment, for example, defines a constant integer value:
public class CONST { public static final int OK = 0; public CONST {} }
This value can be referenced as CONST.OK in any other class without creating an instance of CONST.
The keywords this and super can be used within a class method to refer to the current instance of a class or the instance of its base. (Obviously, super is undefined in classes that do not extend another class.)
The Java object model is very flexible, and it has many subtleties that were not described in this section. This brief discussion was provided only to convey a basic understanding of the model.
Java is an incredibly rich and extensible language, so a complete description is far beyond the scope of this chapter. This section simply presents the basic language constructs and key features, covering datatypes, operators, control statements, and class definition basics. The lexical structure of the language will be illustrated by example.
Table 52.1 lists the built-in Java datatypes and their descriptions.
Table 52.1. Built-in Java datatypes.
Datatype | Description |
byte | 8-bit signed integer |
short | 16-bit signed integer |
int | 32-bit signed integer |
long | 64-bit signed integer |
float | 32-bit single precision floating point |
double | 64-bit double precision floating point |
char | 16-bit integer (Unicode character set) |
boolean | Two logical values (true or false) |
Arrays of basic types can be declared with or without an initial size specification. All the following array declarations are valid:
int valsA[] = new int[10]; short valsB[];