Java Reference
In-Depth Information
A particular object, an instance of
CowboyHat
, will incorporate, or encapsulate, the
owner
, the
size
of the
object, and the status of the hat in the instance variable
hatOn
. Only the constructor, and the
putHatOn()
,
takeHatOff()
,
changeOwner()
, and
getSize()
methods can be accessed externally.
Whenever we are referring to a method in the text, we will add a pair of parentheses
after the method name to distinguish it from other things that have names. Some
examples of this appear in the paragraph above. A method always has parentheses in
its definition and in its use in a program, as we shall see, so it makes sense to represent
it in this way in the text.
Classes and Data Types
Programming is concerned with specifying how data of various kinds is to be processed, massaged,
manipulated or transformed. Since classes define the types of objects that a program will work with, you
can consider defining a class to be the same as defining a data type. Thus
Hat
is a type of data, as is
Tree
, and any other class you care to define. Java also contains a library of standard classes that
provide you with a whole range of programming tools and facilities. For the most part then, your Java
program will process, massage, manipulate or transform class objects.
There are some basic types of data in Java that are not classes, and these are called
primitive
types
. We
will go into these in detail in the next chapter, but they are essentially data types for numeric values
such as 99 or 3.75, for single characters such as 'A' or '?', and for logical values that can be
true
or
false
. Java also has classes that correspond to each of the primitive data types for reasons that we will
see later on so there is an
Integer
class that defines objects that encapsulate integers for instance.
Every entity in your Java program that is not of a primitive data type will be an object of a class - either
a class that you define yourself, a class supplied as part of the Java environment, or a class that you
obtain from somewhere else, such as from a specialized support package.
Classes and Subclasses
Many sets of objects that you might define in a class can be subdivided into more specialized subsets
that can also be represented by classes, and Java provides you with the ability to define one class as a
more specialized version of another. This reflects the nature of reality. There are always lots of ways of
dividing a cake - or a forest.
Conifer
for example could be a subclass of the class
Tree
. The
Conifer
class would have all the instance variables and methods of the
Tree
class, plus some
additional instance variables and/or methods that make it a
Conifer
in particular. You refer to the
Conifer
class as a
subclass
of the class
Tree
, and the class
Tree
as a
superclass
of the class
Conifer
.
When you define a class such as
Conifer
using another class such as
Tree
as a starting point, the class
Conifer
is said to be
derived
from the class
Tree
, and the class
Conifer
inherits
all the attributes of
the class
Tree
.