Java Reference
In-Depth Information
Chapter
1
Bags
Contents
The Bag
A Bag's Behaviors
Specifying a Bag
An Interface
Using the ADT Bag
Using an ADT Is Like Using a Vending Machine
Prerequisites
Appendix C
Creating Classes from Other Classes
Appendix D
Designing Classes
Objectives
After studying this chapter, you should be able to
•
Describe the concept of an abstract data type (ADT)
•
Describe the ADT bag
•
Use the ADT bag in a Java program
T
his chapter builds on the concepts of encapsulation and data abstraction presented
in Appendix D, and it develops the notion of an abstract data type. As you probably
know, a
data type
such as
int
or
double
is a group of values and operations on those
values that is defined within a specific programming language. In contrast, an
abstract data type
, or
ADT
, is a specification for a group of values and the
operations on those values that is defined conceptually and independently of any
programming language. A
data structure
is an implementation of an ADT within a
programming language.
This chapter also begins to generalize the idea of grouping objects. A
collection
is an object that groups other objects and provides various services to its client. In
particular, a typical collection enables a client to add, remove, retrieve, and query the
objects it represents. Various collections exist for different purposes. Their behaviors