System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə9/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   5   6   7   8   9   10   11   12   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

Inheritance is the process by which one object can acquire the properties of another
object. This is important because it supports the concept of hierarchical classification. If
you think about it, most knowledge is made manageable by hierarchical (i.e., top­down)
classifications. For example, a Red Delicious apple is part of the classification apple,
which in turn is part of the fruit class, which is under the larger class food. That is, the
food class possesses certain qualities (edible, nutritious, etc.) which also, logically,
apply to its subclass, fruit. In addition to these qualities, the fruit class has specific
characteristics (juicy, sweet, etc.) that distinguish it from other food. The apple class
defines those qualities specific to an apple (grows on trees, not tropical, etc.). A Red
Delicious apple would, in turn, inherit all the qualities of all preceding classes, and
would define only those qualities that make it unique.


Without the use of hierarchies, each object would have to explicitly define all of its
characteristics. Using inheritance, an object need only define those qualities that make
it unique within its class. It can inherit its general attributes from its parent. Thus, it is
the inheritance mechanism that makes it possible for one object to be a specific
instance of a more general case.
THE JAVA DEVELOPMENT KIT
Now that the theoretical underpinning of Java has been explained, it is time to start
writing Java programs. Before you can compile and run those programs, you must have
a Java Development Kit (JDK). At the time of this writing, the current release of the
JDK is JDK 11. This is the version for Java SE 11. (SE stands for Standard Edition.) It is
also the version described in this book. Because JDK 11 contains features that are not
supported by earlier versions of Java, it is recommended that you use JDK 11 (or later)
to compile and run the programs in this book. (Remember, because of Java’s faster
release schedule, JDK feature releases are expected at six­month intervals. Thus, don’t
be surprised by a JDK with a higher release number.) However, depending on the
environment in which you are working, an earlier JDK may already be installed. If this
is the case, then newer Java features will not be available.
If you need to install the JDK on your computer, be aware that for modern versions of
Java, both Oracle JDKs and open source OpenJDKs are available for download. In
general, you should first find the JDK you want to use. For example, at the time of this
writing, the Oracle JDK can be downloaded from
www.oracle.com/technetwork/java/javase/downloads/index.html
. Also at
the time of this writing, an open source version is available at 
jdk.java.net
. Next,
download the JDK of your choice and follow its instructions to install it on your
computer. After you have installed the JDK, you will be able to compile and run
programs.
The JDK supplies two primary programs. The first is javac, which is the Java compiler.
The second is java, which is the standard Java interpreter and is also referred to as the
application launcher. One other point: The JDK runs in the command­prompt
environment and uses command­line tools. It is not a windowed application. It is also
not an integrated development environment (IDE).
Ask the Expert


Q
: You state that object­oriented programming is an effective way to
manage large programs. However, it seems that it might add
substantial overhead to relatively small ones. Since you say that all
Java programs are, to some extent, object­oriented, does this impose a
penalty for smaller programs?
A
: No. As you will see, for small programs, Java’s object­oriented features are
nearly transparent. Although it is true that Java follows a strict object model, you
have wide latitude as to the degree to which you employ it. For smaller programs,
their “object­orientedness” is barely perceptible. As your programs grow, you will
integrate more object­oriented features effortlessly.
NOTE
In addition to the basic command­line tools supplied with the JDK, there are several
high­quality IDEs available for Java, such as NetBeans and Eclipse. An IDE can be very
helpful when developing and deploying commercial applications. As a general rule, you
can also use an IDE to compile and run the programs in this book if you so choose.
However, the instructions presented in this book for compiling and running a Java
program describe only the JDK command­line tools. The reasons for this are easy to
understand. First, the JDK is readily available to all readers. Second, the instructions
for using the JDK will be the same for all readers. Furthermore, for the simple
programs presented in this book, using the JDK command­line tools is usually the
easiest approach. If you are using an IDE, you will need to follow its instructions.
Because of differences between IDEs, no general set of instructions can be given.
A FIRST SIMPLE PROGRAM
Let’s start by compiling and running the short sample program shown here:


You will follow these three steps:
1. Enter the program.
2. Compile the program.
3. Run the program.
Entering the Program
The programs shown in this book are available from 
www.oraclepressbooks.com
.
However, if you want to enter the programs by hand, you are free to do so. In this case,
you must enter the program into your computer using a text editor, not a word
processor. Word processors typically store format information along with text. This
format information will confuse the Java compiler. If you are using a Windows
platform, you can use WordPad or any other programming editor that you like.
For most computer languages, the name of the file that holds the source code to a
program is arbitrary. 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 Example.java. Let’s see why.
In Java, a source file is officially called a compilation unit. It is a text file that contains
(among other things) one or more class definitions. (For now, we will be using source
files that contain only one class.) The Java compiler requires that a source file use the
.java filename extension. 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 the main 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. The reason for this is that Java is


case sensitive. At this point, the convention that filenames correspond to class names
may seem arbitrary. However, this convention makes it easier to maintain and organize
your programs. Furthermore, as you will see later in this book, in some cases, it is
required.
Compiling the Program
To compile the Example program, execute the compiler, javac, specifying the name of
the source file on the command line, as shown here:
The javac compiler creates a file called Example.class that contains the bytecode
version of the program. Remember, bytecode is not executable code. Bytecode must be
executed by a Java Virtual Machine. Thus, the output of javac is not code that can be
directly executed.
To actually run the program, you must use the Java interpreter, java. To do so, pass the
class name Example as a command­line argument, as shown here:
When the program is run, the following output is displayed:
When Java source code is compiled, each individual class is put into its own output file
named after the class and using the .class extension. This is why it is a good idea to
give your Java source files the same name as the class they contain—the name of the
source file will match the name of the .class file. When you execute the Java
interpreter as just shown, you are actually specifying the name of the class that you
want the interpreter to execute. It will automatically search for a file by that name that
has the .class extension. If it finds the file, it will execute the code contained in the
specified class.
Before moving on, it is important to mention that beginning with JDK 11, Java provides
a way to run some types of simple programs directly from a source file, without
explicitly invoking javac. This technique, which can be useful in some situations, is
described in 
Appendix C
. For the purposes of this book, it is assumed that you are using
the normal compilation process just described.


NOTE
If, when you try to compile the program, the computer cannot find javac (and
assuming that you have installed the JDK correctly), you may need to specify the path
to the command­line tools. In Windows, for example, this means that you will need to
add the path to the command­line tools to the paths defined for the PATH
environmental variable. For example, if JDK 11 was installed under the Program Files
directory, then the path to the command­line tools will be similar to C:\Program
Files\Java\jdk­11\bin. (Of course, you will need to find the path to Java on your
computer, which may differ from the one just shown. Also the specific version of the
JDK may differ.) You will need to consult the documentation for your operating system
on how to set the path, because this procedure differs between OSes.
The First Sample Program Line by Line
Although Example.java is quite short, it includes several key features that are
common to all Java programs. Let’s closely examine each part of the program.
The program begins with the following lines:
This is a comment. Like most other programming languages, Java lets you enter a
remark into a program’s source file. The contents of a comment are ignored by the
compiler. Instead, a comment describes or explains the operation of the program to
anyone who is reading its source code. In this case, the comment describes the program
and reminds you that the source file should be called Example.java. Of course, in real
applications, comments generally explain how some part of the program works or what
a specific feature does.
Java supports three styles of comments. The one shown at the top of the program is
called a multiline comment. This type of comment must begin with /* and end with */.
Anything between these two comment symbols is ignored by the compiler. As the name
suggests, a multiline comment may be several lines long.


The next line of code in the program is shown here:
This line uses the keyword class to declare that a new class is being defined. As
mentioned, the class is Java’s basic unit of encapsulation. Example is the name of the
class. The class definition begins with the opening curly brace ({) and ends with the
closing curly brace (}). The elements between the two braces are members of the class.
For the moment, don’t worry too much about the details of a class except to note that in
Java, all program activity occurs within one. This is one reason why all Java programs
are (at least a little bit) object­oriented.
The next line in the program is the single­line comment, shown here:
This is the second type of comment supported by Java. A single­line comment begins
with a // and ends at the end of the line. As a general rule, programmers use multiline
comments for longer remarks and single­line comments for brief, line­by­line
descriptions.
The next line of code is shown here:
This line begins the main( ) method. As mentioned earlier, in Java, a subroutine is
called a method. As the comment preceding it suggests, this is the line at which the
program will begin executing. In general, Java applications begin execution by calling
main( ). The exact meaning of each part of this line cannot be given now, since it
involves a detailed understanding of several other of Java’s features. However, since
many of the examples in this book will use this line of code, let’s take a brief look at
each part now.
The public keyword is an access modifier. An access modifier determines how other
Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   5   6   7   8   9   10   11   12   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə