Object-Oriented Programming with Java:
This book is designed for those readers who wish to
start learning to program in an object-oriented programming language.
It has been designed primarily as a first programming text.
It is also suitable for those who already have some experience
with another programming language, and who now
wish to move on to an object-oriented one.
Indeed, much of the material is based on courses delivered by the author to
students with a wide range of both
non-programming and programming backgrounds.
The language we use to teach object-oriented programming is Java.
Since its arrival on the scene in 1995, the adoption of Java
as a primary programming language has been amazing.
In its favor at the time of its arrival
were the facts that it was an object-oriented language, and that
it offered a safer and more portable alternative to other languages.
It also rode the wave of interest in the World Wide Web, with which it
integrated well in its provision of applets.
Since then, however, Java has come to be regarded as a genuine mainstream
Our approach in this book is to regard Java as a
language that readers will want to use as a primary tool in
many different areas of their programming work - not just for creating
programs with graphical content within Web pages.
For this reason, in the early chapters
we have avoided an emphasis on creating applets and
While being able to create GUI-based programs is superficially attractive,
concepts required to create them properly are, in fact, quite advanced.
Nevertheless, we recognize that visual examples are much more fun to create
and work with.
To this end, many of our early examples and exercises
are enhanced by the provision of
visual material that makes them more interesting to experiment with.
An object-oriented language makes this approach relatively easy,
without the reader needing to become enmeshed in the details of how
they are implemented.
The following are key features of this book:
An 'objects-early' approach; showing how to interact with
fully-fledged objects, before moving on, in Chapter 4,
to define classes from scratch.
Frequent in-place exercises and reviews.
A thorough glossary, explaining many of the highlighted items of
terminology found in the text.
An accessible introduction to the fundamental object-oriented
topics of polymorphism and inheritance.
Significant coverage of the many GUI classes belonging to both the
Abstract Windowing Toolkit (AWT) and Swing (JFC),
which support both standalone applications and applets.
Up-to-date coverage of the Java 2 Platform API.
How to use the power of threads for multi-threaded programs, while
avoiding hazards such as deadlock, livelock and thread starvation.
Timely coverage of networking, via TCP/IP, to interact with non-Java programs.
A unique chapter on event-driven simulation.
In Chapter 2 through Chapter 15,
we cover the most important features of object-oriented programming
and the Java language.
Within those chapters, exercises have been
deliberately positioned between sections, rather than grouped at the end.
We recommend that these exercises are attempted at the point they are reached,
because many of them are designed to reinforce important concepts
that you should feel confident with before moving forward.
Each of these chapters also includes periodic reviews to reprise and
reinforce the main points of the
material covered in the preceding sections.
From time to time, case studies are used in order to reinforce or bring
out further points that are best made in looking at the
design and implementation of a larger problem.
In the remaining chapters, we describe in detail how to use many of the
GUI components provided by the AWT and Swing classes, how to write multi-threaded
programs and how to interact with programs across a network.
We conclude with a chapter on simulation - a common application area for
Throughout the book, there is an emphasis on the importance of good programming
style; particularly the need to maintain an object's integrity from outside
The individual chapters are organized as follows.
The Java API continues to grow, and it is impossible to cover it all in
detail within the scope of a teaching text such as this.
In Chapter 15,
through Chapter 20, therefore,
we only attempt to sample the riches of the classes it
defines, and provide illustrations of something of what is possible with them.
By that stage, our hope is that the reader will be able to
harness the power that object-oriented programming in Java permits, and be
able to create their own interesting and practical everyday programs.
Chapter 1 provides an introduction to the basics of computer
systems, for those with little or no experience of using a computer to write
We present a simple model of a computer and its components,
in order to provide a
foundation for the ways in which the design of
most programming languages is shaped by them.
Experienced programmers will find that they can skip this chapter.
In Chapter 2, we describe the fundamental elements of the
Java language, and show how to compile and run a simple program.
In Chapter 3, we provide
an introduction to basic object-oriented concepts, such as classes and objects.
The approach taken in this chapter is fundamental to that taken in the rest of
the book: objects are important.
We believe that an `objects-early' approach is the best way to learn an
At this stage, the emphasis is on using existing classes
rather than defining them.
This approach helps to emphasize that objects communicate by passing
messages to one another.
In Chapter 4, we begin to show how simple
classes may be defined from scratch.
We discuss the ways in which objects use attributes to
maintain their state and the importance of protecting
those attributes from inappropriate modification.
It is at this early stage
that the concepts of accessors, mutators and
encapsulation are introduced.
In Chapter 5, we
start to introduce the core features of Java
that enable behavior to be added to class definitions.
We concentrate on straight-line sequences of statements,
and discuss arithmetic expressions.
In Chapter 6, we continue to add behavior to classes
but introduce statements that allow objects to perform alternative sequences of
We include a discussion of Boolean expressions, which are used
to control this behavior.
In Chapter 7, the ability to repeat statements
is added to class definitions.
By the end of this chapter, we are able to use the three fundamental
elements of behavior - sequence, choice and repetition.
In Chapter 8, we introduce packages - Java's
means of grouping related classes.
We include a description of several pre-defined classes that are used over and
over again in programs:
In Chapter 9, we describe array objects, which make
it possible to group related objects or items of data into fixed-sized
In Chapter 10, we introduce classes that support
arbitrary-sized collections of objects.
While most programming languages provide array
facilities, Java adds standard collection classes
that other languages often leave to programmers to create for themselves.
In Chapter 11, we explore Java's exception-handling
This provides a way for objects to indicate when an unexpected situation has
arisen with which they do not know how to deal.
Complementary to this is the ability to `catch' exceptions and develop
work-arounds for the problems encountered.
In Chapter 12,
we look at some of the many ways in which a program can
interact with the external file system, via pre-defined input-output classes.
In Chapter 13, we investigate
In earlier chapters we use the term `public interface' informally, but
this chapter explores the issue in more depth.
Central to it is the concept of polymorphism.
Since polymorphism is such a
fundamental concept in any object-oriented language, we continue our
discussion of it in the next chapter.
In Chapter 14, we
take the discussion of polymorphism one stage further by describing Java's
extends facility for class inheritance.
provides the ability to derive new classes that inherit much of their code
and functionality from existing classes. Among other things,
this permits code re-use.
As part of the material in this chapter,
we discuss alternatives to inheritance and
In Chapter 15,
we look at abstract classes, nested
classes and nested interfaces.
By the end of this chapter, we have covered most of the main features of the
Apart from Chapter 18, therefore,
the remaining chapters are largely
concerned with using these features in different applications; such as
graphical programming, networking and simulation.
In Chapter 16, we describe the classes of the
Abstract Windowing Toolkit (AWT).
The AWT provides a powerful collection of classes
that simplify the creation of applications with graphical user interfaces.
They free a programmer from a need to know
details of the windowing environment in which their programs will be run.
For the sake of generality,
we concentrate on standalone applications, rather than applets.
Applets are covered, separately, in Chapter 20.
In Chapter 17, we continue the description of Java's
This chapter covers the Swing classes, which
build on the AWT facilities to provide a more complex set of classes,
and greater programmer control over the look-and-feel of a program.
In Chapter 18, we introduce the final major
language feature: threads.
Threads are a powerful feature that
make it possible to create objects that run concurrently, sharing the
available processor time between them.
However, the power of threads brings with it a number of new problems of
which the designer of multithreaded programs
needs to be aware. We discuss problems such as race hazards,
starvation, livelock and deadlock, and how to avoid them.
In Chapter 19, we introduce the classes that allow
a program to interact with other programs across a network.
We discuss use of the reliable TCP protocol and the unreliable
UDP protocol, both of which are well supported by Java's socket classes.
In Chapter 20, we return to features of graphical programming,
with a description of applets.
Applets are largely restricted forms of the stand-alone applications
described in Chapter 16.
They are provided with a secure environment which prevents them from
directly accessing the host machine on which they are run.
These restrictions apply so that they can be run within browsers, providing
the familiar active content of Web pages.
Chapter 21, concludes the main content of the book
with a description of the basic elements of
event-driven (discrete) simulation.
Simulations represent an important set of object-oriented programs
and we describe them through the presentation of two case studies.
The Appendices provide additional background material on primitive data
types, number representations, operators and a list of Java's reserved words.
Some recurring design patterns are discussed in Appendix E
and our main stylistic conventions are summarized in
The book concludes with a comprehensive glossary of terminology.
The examples we use have been developed using the Java 2 SDK,
which is freely available from the
JavaSoft Web site.
You are strongly recommended to obtain a copy of the documentation on the Java
API which is available from the JavaSoft site.
The source code for the
examples in this book is available from the publisher's Companion Website or
from here either
via HTTP or
Material for instructors are also available. These include solutions to sample
exercises taken from Chapters 1 to 15, and PowerPoint slides for the same
set of chapters.
Taken from the book,
Oriented Programming with Java:
An Introduction, by
David J. Barnes.
Published by Prentice Hall, January 2000.
This document (http://www.cs.kent.ac.uk/~djb/oop/preface.html)
is maintained by:
to whom any comments and corrections should be addressed.
© 1999-2000 David Barnes and Prentice-Hall.
Last updated: 7th February 2000