Managing data is what programming is all about. We group information about
an entity together in records or data structures. The record or structure
definition represents a class of items we want to keep information on.
When we load data into these records or structures we are describing the
attributes of that instance or item of that class of items.
In conventional programming we write procedures or functions to manipulate
the data (loading, moving, reading, writing, etc.). These functions and
procedures represent the methods by which we manipulate the data.
The functions and procedures stand alone and are not connected to the data
structures other than the fact that they use the structures.
Object Oriented Programming (OOP) simply couples the data structures
with the procedures used to manipulate the data in the structures. The
data items become the attributes of the object and the procedures or
functions that manipulate the data become the methods of the object.
This grouping of attributes and methods is called
encapsulation and is the first cornerstone of OOP.
The values of the attributes (the data) determine the state
of the object. The state the object is in determines the uniqueness
of that instance of the class of objects.
The methods determine the behavior of a class of
objects. Notice that the behaviors are the same for all instances of
the class, where the attributes are unique to each instance of the
class.
Another cornerstone of OOP is the concept of data hiding. This
simply means that only the instance of the object class has access to its
data. The object is solely responsible for maintaining its own data.
If data hiding is to work we must establish a new conceptual description
of the object.
The data or attributes are part of the implementation
of the object. This is where the state of the object is defined and
only the object itself has access to this area. Everything in this area is private.
Only the object itself knows the details of its implementation.
The methods define the interface of the object with the
outside world (other objects and the rest of the program). The interface is
public. Before we mislead let us state that the methods
are part of the implementation. The interface is the part of
the method that is visible to the outside world. This is the method
header (each method has a header and a body). The method body (the actual
code for the function) describes the behavior of the object and is part
of the implementation. This idea of separating implementation and
interface is another cornerstone of OOP.
Since the object is solely responsible for what state it is in (the
values of its data items), it must insure that whatever state it is in is
a valid state at all times. This is the job that the methods perform.
The methods must check all data values for correctness before setting any
attributes based on that data. This is the basis of what I call the
Prime Directive.