|
|
|
|||
|
Creating ClassesNow that we've covered how to create, use, and destroy objects, it's time to show you how to write the classes from which objects are created. This section shows you the main components of a class through a small example that implements a last-in-first-out (LIFO) stack. The following diagram lists the class and identifies the structure of the code.![]() This figure has been reduced to fit on the page. Click the image to view it at its natural size. Vector,
to store its elements.
Vector is a growable array of objects
and does a nice job of allocating space
for new objects as space is required.
The Stack class makes use of this code
by using a Vector to store its elements.
However, it imposes LIFO restrictions
on the Vector--
that is, you can only add elements to
and remove elements from the top of the stack.
The Class DeclarationThe previous diagram shows that two primary components make up the implementation of a class: the class declaration and the class body. The class declaration declares the name of the class along with other attributes. The class declaration for the The Class BodyThe class body follows the class declaration and is embedded within curly braces { and }. The class body contains declarations for all instance variables and class variables (known collectively as member variables) for the class. In addition, the class body contains declarations and implementations for all instance methods and class methods (known collectively as methods) for the class. Providing Constructors for Your ClassesA class may contain one or more constructors that provide for the initialization of an object created from the class. This method shows you how to write a constructor. Declaring Member VariablesA class's state is represented by its member variables. You declare a class's member variables in the body of the class. Typically, you declare a class's variables before you declare its methods, although this is not required.Note: To declare variables that are members of a class, the declarations must be within the class body, but not within the body of a method. Variables declared within the body of a method are local to that method. Implementing MethodsAs you know, objects have behavior that is implemented by its methods. Other objects can ask an object to do something by invoking its methods. This section tells you everything you need to know about writing methods for your Java classes. For more information about how to call methods see Using Objects. Controlling Access to Members of a ClassMember variables and methods are known collectively as members. When you declare a member of a Java class, you can allow or disallow other objects of other types access to that member through the use of access specifiers. Understanding Instance and Class MembersA Java class can contain two different types of members: instance members and class members. This page shows you how to declare both types of members and how to use them. Writing a
| |||
|