Java Reference
In-Depth Information
internals of the class without necessitating changes to programs that use the class. As long as the external
characteristics of the methods that can be called from outside the class remain unchanged, the internal code
can be changed in any way that you, the programmer, want.
A particular object, an instance of CowboyHat , incorporates, or encapsulates, 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.
NOTE Whenever I am referring to a method in the text, I add a pair of parentheses after the
method name to distinguish it from other things that have names. Some examples of this ap-
pear in the preceding paragraph. A method always has parentheses in its definition and in its
use in a program, as you'll 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, manip-
ulated, or transformed. Classes define the types of objects that a program works with so 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 processes, massages, manip-
ulates, or transforms class objects.
There are some basic types of data in Java that are not classes, and these are called primitive types . I 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 you see later. There is an Integer
class that defines objects that encapsulate integers, for example. Every entity in your Java program that is
not of a primitive data type is 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 different classes, and Java provides you with the capability 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 .
Search WWH ::




Custom Search