To make this discussion of objects concrete, let us consider an example object. Specifically, we'll consider the user interface component known as a text field. A text field is just an area on the screen where the user can enter text (e.g. the "Netsite:" bar at the top of Netscape is a text field). The data of a text field object would contain things like: the actual text in the field, the color of that text, and the font the text is displayed in. The methods of the text field object would be things like: change the text to some new text, clear the text, change the color, etc. Pseudocode for a text field object would look something like this (the following is not quite Java):
In a fully object-oriented language like Java, you build a program just by describing a variety of different objects. Your Java program (sometimes called an applet) is itself an object. Other objects in your program might be a window object (for displaying windows on screen), a database object (for maintaining chunks of related data, such as student's grades), or a user-generated event (such as a mouse click).Object TextField { // These variables contain the data of the object Font myFont; Color myColor; String myText; // The methods below perform operations on the text field Method SetText (String newText) { // set myText to the new text myText = newText; } Method Clear () { // set myText to the empty string myText = ""; } Method ChangeColor (Color newColor) { // set myColor to newColor myColor = newColor ; } }
Each different kind of object is called a class. So, for instance, you could define a textField class, which would tell the computer what kind of information and actions a text field object would contain. Then, you could create any number of text field objects. These objects could each contain different actual data (e.g. different values in their myText string), but they all contain the same kind of information (myText, myFont, myColor) as well as the same methods. Thus, in some sense, a class is a template for an object, and objects are the specific instances of a class.
Another property of object-oriented languages is that they allow you to "derive" one class from another. Basically what this means is that, given one class, you can describe a second class as an extension of the first.
For instance, let's say you want to make a new class, based on your textField class, that is simply a text field plus a scroll bar. You will still want to store the same data and perform the same operations as the textField class, but in addition, you want to add some data for a scroll bar and some methods for controlling it. An object-oriented language will allow you to create this new class very simply. The pseudo-code for the definition of a "text field plus scroll bar" class might look something like this:
Class ScrollBarTextField derived from TextField { // Add the scroll bar data ScrollBar myScroll; Method ShowScrollBar() { // tell the scroll bar to show itself myScrollBar.show(); } Method HideScrollBar() { // tell the scroll bar to hide itself myScrollBar.hide(); } }
Notice that this new class definition makes no mention of the myFont, myColor, or myText data, nor of the SetText, Clear, or ChangeColor methods. However, an object which is of the ScrollBarTextField class still has all of those features; you just don't have to explicitly declare them, because ScrollBarTextField was derived from TextField. The new data and methods become part of the new class through a process called inheritance.
PREVIOUS | 1 | 2 | 3 | 4 | 5 | 6 | 7 | NEXT |