One of the fundamental ways in which we handle complexity is 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 endeavour is finding the right abstractions.
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 definition is a variable which can store a value that represents a particular property. 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 definition 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.
As an example, we will implement different versions of a class that models the abstraction of a stack that can push and pop characters. The stack will use an array of characters to store the characters, and a field to indicate the top element in the stack. Using Unified Modeling Language (UML) notation, a class called CharStack is graphically depicted in Figure 1.1, which models the abstraction. Both fields and method names are shown in Figure 1.1a.
Example 1.1 shows the definition of the class CharStack depicted in Figure 1.1. Its intention is to illustrate the salient features of a class definition in Java, and not effective implementation of stacks.
A class definition consists of a series of member declarations. In the case of the class CharStack, it has two fields:
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., index of the last character stored in the array)
The class CharStack has five methods that implement the essential operations on a stack:
push() pushes a character on to the stack
pop() removes and returns the top element of the stack
peek() returns the top element of the stack for inspection
isEmpty() determines whether the stack is empty
isFull() determines whether the stack is full
The class definition also has a method-like declaration with the same name as the class, (2). Such declarations are called constructors. As we shall see, a constructor is executed when an object is created from the class. However, the implementation details in the example are not important for the present discussion.
// Source Filename: CharStack.java public class CharStack { // Class name // Class Declarations: // (1) Fields: private char[] stackArray; // The array implementing the stack. private int topOfStack; // The top of the stack. // (2) Constructor: public CharStack(int n) { stackArray = new char[n]; topOfStack = -1; } // (3) Methods: public void push(char element) { stackArray[++topOfStack] = element; } public char pop() { return stackArray[topOfStack--]; } public char peek() { return stackArray[topOfStack]; } public boolean isEmpty() { return topOfStack < 0; } public boolean isFull() { return topOfStack == stackArray.length - 1; } }