Sunday, January 8, 2012

What Defines a Class of Objects?

A class definition identifies all the parameters that define an object of that particular class, at least, so far as your needs go. Someone else might define the class differently, with a larger or smaller set of parameters to define the same sort of object - it all depends on what you want to do with the class. You decide what aspects of the objects you include to define that particular class of object, and you choose them depending on the kinds of problems that you want to address using the objects of the class. Let’s think about a specific class of objects.

If you were defining a class Hat, for example, you might use just two parameters in the definition. You could include the type of hat as a string of characters such as “Fedora” or “Baseball cap” and its size as a numeric value. The parameters that define an object of a class are referred to as instance variables or attributes of a class, or class fields. The instance variables can be basic types of data such as numbers, but they can also be other class objects. For example, the name of a Hat object could be of type String - the class String defines objects that are strings of characters.

Of course there are lots of other things you could include to define a Hat if you wanted to, color, for example, which might be another string of characters such as “Blue”. To specify a class you just decide what set of attributes meet your requirements, and those are what you use. This is called data abstraction in the parlance of the object-oriented aficionado because you just abstract the attributes you want to use from the myriad possibilities for a typical object. In Java the definition of the class Hat would look something like this:

class Hat {
// Stuff defining the class in detail goes here.
// This could specify the name of the hat, the size,
// maybe the color, and whatever else you felt was necessary.
}

The name of the class follows the word class, and the details of the definition appear between the curly braces. I won’t go into the detail of how the class Hat is defined, since you don’t need it at this point. The lines appearing between the braces above are not code; they are actually program comments, because they begin with two successive forward slashes. The compiler will ignore anything on a line that follows two successive forward slashes in your Java programs, so you’ll use this to add explanations to your programs. Generally, the more useful comments you can add to your programs, the better. You will see in next post that there are other ways you can write comments in Java.

Each object of your class will have a particular set of values defined that characterize that particular object. You could have an object of type CowboyHat, which might be defined by values such as “Stetson” for the type of the hat, “White” for the color, and the size as 7. Although shows CowboyHat objects defined by a set of three values that you would not normally expect to change for a given instance, in general the parameter values that define an object are not necessarily fixed. You would expect the type and size attributes for a particular CowboyHat object to stay fixed since hats don’t usually change their size - at least, not unless it’s raining - but you could have other attributes.

You might have a parameter owner, which would record the owner’s name, so the value stored as the attribute owner could be changed when the hat was sold or otherwise transferred to someone else. You might also have a parameter hatOn, for example, which would indicate whether the hat was on or off the owner’s head; the value true would indicate that the owner was indeed wearing the hat, whereas the value false would mean that the hat had been removed and was just lying about somewhere.

No comments:

Post a Comment

Thanks for Commenting......