Java Reference
In-Depth Information
data. Java uses a class specification to construct
objects
. Objects are instances of a class.
Thus, a class is essentially a set of plans that specify how to build an object.
The code and data that constitute a class are called
members
of the class. Specifically,
the data defined by the class are referred to as
member variables
or
instance variables
. The
code that operates on that data is referred to as
member methods
or just
methods. Method
is
Java's term for a subroutine. If you are familiar with C/C++, it may help to know that what
a Java programmer calls a
method
, a C/C++ programmer calls a
function
.
Polymorphism
Polymorphism
(from Greek, meaning “many forms”) is the quality that allows one interface
to access a general class of actions. The specific action is determined by the exact nature
of the situation. A simple example of polymorphism is found in the steering wheel of an
automobile. The steering wheel (i.e., the interface) is the same no matter what type of ac-
tual steering mechanism is used. That is, the steering wheel works the same whether your
car has manual steering, power steering, or rack-and-pinion steering. Therefore, once you
know how to operate the steering wheel, you can drive any type of car.
The same principle can also apply to programming. For example, consider a stack (which
is a first-in, last-out list). You might have a program that requires three different types of
stacks. One stack is used for integer values, one for floating-point values, and one for char-
acters. In this case, the algorithm that implements each stack is the same, even though the
data being stored differs. In a non-object-oriented language, you would be required to cre-
ate three different sets of stack routines, with each set using different names. However, be-
cause of polymorphism, in Java you can create one general set of stack routines that works
for all three specific situations. This way, once you know how to use one stack, you can use
them all.
More generally, the concept of polymorphism is often expressed by the phrase “one in-
terface, multiple methods.” This means that it is possible to design a generic interface to
a group of related activities. Polymorphism helps reduce complexity by allowing the same
interface to be used to specify a
general class of action
. It is the compiler's job to select
the
specific action
(i.e., method) as it applies to each situation. You, the programmer, don't
need to do this selection manually. You need only remember and utilize the general inter-
face.
Inheritance
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) classifica-
tions. For example, a Red Delicious apple is part of the classification
apple
, which in turn