Java Reference
In-Depth Information
Of course, for each type of object you can have any operation that makes sense for you. If you want to
have a shootHoleIn operation for Hat objects, that's no problem. You just have to define what that opera-
tion does to an object.
You are probably wondering at this point how an operation for a class is defined. As you see in detail a
bit later, it boils down to a self-contained block of program code called a method that is identified by the
name you give to it. You can pass data items — which can be integers, floating-point numbers, character
strings, or class objects — to a method, and these are processed by the code in the method. A method may
also return a data item as a result. Performing an operation on an object amounts to executing the method
that defines that operation for the object.
WARNING Of course, the only operations you can perform on an instance of a particular
class are those defined within the class, so the usefulness and flexibility of a class is going to
depend on the thought that you give to its definition. We look into these considerations more
in Chapter 5.
Just so you can recognize one when you see it, let's take a look at an example of a complete class defini-
tion. The code for the class CowboyHat we have been talking about might be as illustrated in Figure 1-7 .
FIGURE 1-7
 
 
Search WWH ::




Custom Search