As you can see, it is through the application of encapsulation, inheritance, and
polymorphism that the individual parts are transformed into the object known as a car.
The same is also true of computer programs. By the application of object-oriented principles,
the various parts of a complex program can be brought together to form a cohesive, robust,
maintainable whole.
As mentioned at the start of this section, every Java program is object-oriented. Or, put
more precisely, every Java program involves encapsulation, inheritance, and polymorphism.
Although the short example programs shown in the rest of this chapter and in the next few
chapters may not seem to exhibit all of these features, they are nevertheless present. As you
will see, many of the features supplied by Java are part of its built-in class libraries, which
do make extensive use of encapsulation, inheritance, and polymorphism.
A First Simple Program
Now that the basic object-oriented underpinning of Java has been discussed, let's look at
some actual Java programs. Let's start by compiling and running the short sample program
shown here. As you will see, this involves a little more work than you might imagine.
This is a simple Java program.
Call this file "".
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
NOTE  The descriptions that follow use the standard Java SE 6 Development Kit (JDK 6), which is
available from Sun Microsystems. If you are using a different Java development environment,
then you may need to follow a different procedure for compiling and executing Java programs.
In this case, consult your compiler's documentation for details.
Entering the Program
For most computer languages, the name of the file that holds the source code to a program
is immaterial. However, this is not the case with Java. The first thing that you must learn
about Java is that the name you give to a source file is very important. For this example,
the name of the source file should be Let's see why.
In Java, a source file is officially called a compilation unit. It is a text file that contains one
or more class definitions. The Java compiler requires that a source file use the .java filename
As you can see by looking at the program, the name of the class defined by the program
is also Example. This is not a coincidence. In Java, all code must reside inside a class. By
convention, the name of that class should match the name of the file that holds the program.
You should also make sure that the capitalization of the filename matches the class name.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks