Abstractions is one of the fundamental ways in which we handle complexity. 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, and 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.
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, which models the abstraction.
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:
-
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.



Join The Community