03-01-2013, 12:29 PM
A Programmer’s Guide to Java™ SCJP Certification
1A Programmer’s Guide.pdf (Size: 6.94 MB / Downloads: 326)
Introduction
Before embarking on the road to Java programmer certification, it is important to
understand the basic terminology and concepts in object-oriented programming
(OOP). In this chapter, the emphasis is on providing an introduction rather than
exhaustive coverage. In-depth coverage of the concepts follows in subsequent
chapters of the book.
Java supports the writing of many different kinds of executables: applications,
applets, and servlets. The basic elements of a Java application are introduced in
this chapter. The old adage that practice makes perfect is certainly true when learning
a programming language. To encourage programming on the computer, the
mechanics of compiling and running a Java application are outlined.
Classes
One of the fundamental ways in which we handle complexity is in abstractions. An
abstraction denotes the essential properties and behaviors of an object that
differentiate it from other objects. The essence of OOP is modelling abstractions,
using classes and objects. The hard part in this endeavor is finding the right
abstraction.
A class denotes a category of objects, and acts as a blueprint for creating such
objects. A class models an abstraction by defining the properties and behaviors for
the objects representing the abstraction. An object exhibits the properties and
behaviors defined by its class. The properties of an object of a class are also called
attributes, and are defined by fields in Java. A field in a class is a variable which can
store a value that represents a particular property of an object. The behaviors of an
object of a class are also known as operations, and are defined using methods in Java.
Fields and methods in a class declaration are collectively called members.
An important distinction is made between the contract and the implementation that
a class provides for its objects. The contract defines what services, and the implementation
defines how these services are provided by the class. Clients (i.e., other
objects) only need to know the contract of an object, and not its implementation, in
order to avail themselves of the object’s services.
Declaring Members: Fields and Methods
Example 1.1 shows the declaration of the class CharStack depicted in Figure 1.1. Its
intention is to illustrate the salient features of a class declaration in Java, and not
the effective implementation of stacks.
A class declaration consists of a series of member declarations. In the case of the
class CharStack, it has two fields declared at (1):
• stackArray, which is an array to hold the elements of the stack (in this case,
characters)
• topOfStack, which denotes the top element of the stack (i.e., the index of the last
character stored in the array)
Class Instantiation, Reference Values, and References
The process of creating objects from a class is called instantiation. An object is an
instance of a class. The object is constructed using the class as a blueprint and is
a concrete instance of the abstraction that the class represents. An object must be
created before it can be used in a program.
A reference value is returned when an object is created. A reference value denotes a
particular object. An object reference (or simply reference) is a variable that can store
a reference value. A reference thus provides a handle to an object, as it can indirectly
denote an object whose reference value it holds. In Java, an object can only
be manipulated via its reference value, or equivalently by a reference that holds its
reference value.
Instance Members
Each object created will have its own copies of the fields defined in its class. The
fields of an object are called instance variables. The values of the instance variables
in an object comprise its state. Two distinct objects can have the same state, if their
instance variables have the same values. The methods of an object define its behavior.
These methods are called instance methods. It is important to note that these
methods pertain to each object of the class.
Invoking Methods
Objects communicate by message passing. This means that an object can be made
to exhibit a particular behavior by sending the appropriate message to the object.
In Java, this is done by calling a method on the object using the binary infix dot
('.') operator. A method call spells out the complete message: the object that is the
receiver of the message, the method to be invoked, and the arguments to the
method, if any. The method invoked on the receiver can also send information back
to the sender, via a single return value. The method called must be one that is
defined for the object, otherwise the compiler reports an error.