Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This section shows you the main components of a class by using a small example that implements a stack a data structure whose items are added and removed in a last-in-first-out (LIFO) fashion. The following figure shows theStack
class and identifies the elements of a class.A class definition has two main components: the class declaration and the class body. The class declaration is the first line of code in a class. At a minimum, the class declaration declares the name of the class. The class body follows the class declaration and appears between braces
{
and}
. The class body contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the variables that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects. TheStack
class defines two member variables within the class body theitems
list andtop
, the array index of the top of the stack. TheStack
class also defines one constructor which takes one argument and three methods:push
,pop
, andisEmpty
.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.