The implicit characteristic of an object-oriented program is Abstraction. Abstraction is the specification of an abstract data type, which includes a specification of the type’s data representation and behavior. In particular,
- what kind of data can be stored in an entity of the new type, and
- what are all the ways that that data can be manipulated.
For our purposes, an abstract data type is a new type (not intrinsic to the language). It is not one of the primitive data types that are built into the programming language (such as int, long, float, etc.).
The distinction in the previous paragraph is very important. The data representation and behavior of the intrinsic or primitive types is already known to the compiler and cannot be modified by the programmer.
The representation and behavior of an abstract type is not known to the compiler until it is specified by the programmer and presented to the compiler in an appropriate manner.
How do we present the specification of an abstract type to the compiler? Java and C++ programmers define the data representation and the behavior of a new type (present the specification to the compiler) using the keyword class (C++ programmers can also use the keywords struct and union).
In other words, in Java and C++, the keyword class is used to convert the specification of a new type into something that the compiler can work with; a set of plans as it were.
Other languages may use different mechanisms to present the specification of the abstract type to the compiler.
Once the new type is defined, one or more objects of that type can be brought into being (instantiated, caused to occupy memory).
Once instantiated, the object is said to have state and behavior. The state of an object is determined by the current values of its data (instance variables) and the behavior of an object is determined by its methods (member functions or instance methods).
For example, again taking some liberties, if we view a GUI button as an object, it is fairly easy to visualize state and behavior.
A GUI button can usually manifest any of a number of different states: size, position, depressed image, not depressed image, caption, etc. Each of these states is determined by data stored in the instance variables of the button object at any given point in time. (The combination of one or more instance variables that determine a particular state are often referred to as a property of the object.)
Similarly, it is not too difficult to visualize the behavior of a GUI button. When you click it with the mouse, that usually causes some specific action to take place.
In fact, if you dig deeply enough into GUI programming tools, you will probably find that there is a class of button of which each individual button is an instance (object).
Each individual button object has instance variables, the values of which define the state of the button at any given time.
Every instance of the button class has certain fundamental behavior (respond to a click event, respond to a mouse dragover, etc.) which can be used to trigger some higher level action.