Here’s the amazing breakthrough that is the key to OOP: An object contains both
functions and variables. A thermostat object, for example, would contain not only
furnace_on() and furnace_off() functions, but also currentTemp and desiredTemp. Incidentally, before going further we should note that in Java, functions are called methods and variables are called fields.
This new entity, the object, solves several problems simultaneously. Not only does a programming object correspond more accurately to objects in the real world, it also solves the problem engendered by global data in the procedural model. The
furnace_on() and furnace_off() methods can access currentTempand desiredTemp. These variables are hidden from methods that are not part of thermostat, however, so they are less likely to be accidentally changed by a rogue method.
You might think that the idea of an object would be enough for one programming
revolution, but there’s more. Early on, it was realized that you might want to make several objects of the same type. Maybe you’re writing a furnace control program for an entire apartment house, for example, and you need several dozen thermostat objects in your program. It seems a shame to go to the trouble of specifying each one separately. Thus, the idea of classes was born.
A classis a specification—a blueprint—for one or more objects. Here’s how a
thermostat class, for example, might look in Java:
* @purpose: This program is to specify a thermostat class
* @author: hoiquanphanmem
* @date: 12/13/2014
private float currentTemp();
private float desiredTemp();
public void furnace_on()
// method body goes here
public void furnace_off()
// method body goes here
} // end class thermostat
The Java keyword class introduces the class specification, followed by the name you want to give the class; here it’s thermostat. Enclosed in curly brackets are the fields and methods (variables and functions) that make up the class. We’ve left out the body of the methods; normally there would be many lines of program code for each one. C programmers will recognize this syntax as similar to a structure, while C++ programmers will notice that it’svery much like a class in C++, except that there’s no semicolon at the end. (Why did we need the semicolon in C++ anyway?)
Specifying a class doesn’t create any objects of that class. (In the same way specifying a structure in C doesn’t create any variables.) To actually create objects in Java you must use the keyword new. At the same time an object is created, you need to store a reference to it in a variable of suitable type; that is, the same type as the class.
What’s a reference? We’ll discuss references in more detail later. In the meantime, think of it as a name for an object. (It’s actually the object’s address, but you don’t need to know that.)
Here’s how we would create two references to type thermostat, create two new
thermostat objects, and store references to them in these variables:
thermostat therm1, therm2; // create two references
therm1 = new thermostat(); // create two objects and
therm2 = new thermostat(); // store references to them
Incidentally, creating an object is also called instantiating it, and an object is often
referred to as an instance of a class.
Accessing Object Methods
Once you’ve specified a class and created some objects of that class, other parts of your program need to interact with these objects. How do they do that?
Typically, other parts of the program interact with an object’s methods (functions), not with its data (fields). For example, to tell the therm2 object to turn on the furnace, we would say
The dot operator (.) associates an object with one of its methods (or occasionally with one of its fields).
• Objects contain both methods (functions) and fields (data).
• A class is a specification for any number of objects.
• To create an object, you use the keyword new in conjunction with the class name.
• To invoke a method for a particular object you use the dot operator.
These concepts are deep and far-reaching. It’s almost impossible to assimilate them the first time you see them, so don’t worry if you feel a bit confused. As you see more classes and what they do, the mist should start to clear.