|
Java's class structure is made up
of the following major components: classes,
hierarchy, variables, methods, and inheritance.
The key to understanding
Java's object-oriented design is understanding
what classes are and what you can do with them.
Classes are templates that you use to create
actual objects. The instructions contained in a
class are used to create one or more objects,
which can be called instances of classes
in Java. When you create an object from a class,
you instantiate the object, which means
you create an instance of the class.
In object-oriented
programming, you can think of an object as you
would any real-world object, for example, a
rectangle. The actual rectangle would be an
instance of the class Rectangle.
A very rudimentary declaration
of a class is as follows:
class classname {
//class instructions
}
The instructions in a class
are made of two basic components: variables that
hold data, and methods that manipulate the data.
Before you begin creating the
classes in your application, you must design the
class structure. Java's class structure is
organized into a hierarchy.
Classes are organized into a
hierarchy to allow you to easily reuse code.
When you write a Java program, first determine
which objects you'll need to use in your code.
Then determine what variables and methods the
object's class must store. When you know what
instructions your classes contain, plan your
hierarchy.
Without planning your
hierarchy first, you would begin creating a
class for each object individually and
undoubtedly would find that you are repeating
instructions from class to class. In a
hierarchy, instructions common to groups of
objects are separated out in parent classes, or
superclasses, and can be used by all of
their subclasses. Therefore, when you
plan your hierarchy, you would group objects by
the instructions that they have in common and
organize them into a hierarchy.
Superclasses are used as
templates to create subclasses with variables
and methods that make each subclass unique. Each
superclass can be a parent to one or many other
subclasses. Unlike in C++, a subclass can have
only one superclass. Therefore, the Java class
hierarchy looks something like the one shown in
Figure 1.1.
Figure 1.1:
Java class hierarchy.
If a superclass is not defined
when a class is declared, the class is
automatically made a subclass of Java's
Object class. Every class in a Java program
is a descendant of Object. Object
itself has no superclass.
Classes store information that
describe objects in instance variables.
When objects are created from a class, they
contain new instances of the class' variables.
These instance variables can have values that
are different from one object to the next.
Values of instance variables are called data.
When an instance variable's data is changed, it
affects only the individual object. There is a
way in Java to assign a variable to a class
that, if changed, is changed in all instances of
the class. Such a variable is called a static
variable.
The basic declaration
statement of a variable is this:
datatype
variablename;
Two variables that might be
declared for a rectangle are length and
width. They could be declared in one
line because they have the same data type. Their
declaration statement might be this:
int length, width;
If you created an object from
a class with only these two variables declared,
the object would simply hold this data. It would
not know to draw lines with these values to form
a rectangle. Methods must be declared to use the
data in variables.
Methods are functions that
must be associated with individual classes. In C
and C++ and other procedural languages,
functions can be placed anywhere in the code. In
Java, they must be stored within classes.
Instances of methods are created when instances
of classes are created. Unlike variables,
methods are not duplicated in objects-their code
is stored only in the class.
When an object's method is
invoked from its class, it uses the data of
variables in the object.
Every method returns a value
if it is not declared as void. To
declare a method, you use the following
statement:
returntype methodname
(parameter list) {
//method code
}
Variables and methods that are
stored in classes in the class hierarchy are
inherited by subclasses, so you do not need to
re-create them. Objects created from a subclass
will contain not only the instances of the
variables and methods of the subclass, but also
its superclass' variables and methods, as well
as those of the parent of its superclass, and so
on. When a variable or method is referenced in
an object, it is retrieved in a specific order:
Java first searches for it in the current class,
then, if it is not found, it searches the parent
class, and so on.
In summary, objects in your
Java program are created from classes that
contain variables and methods to describe the
object. Classes are organized into a hierarchy
in which classes inherit functionality from
parent classes, which allows for reusable code.
These basic concepts of the structure of Java's
object-oriented programming language will become
clearer to you as you read through the rest of
the sections. |