Lab 8
Page 2


More about Java: Object-Oriented Programming


Object-Oriented Programming

Object-oriented programming (OOP) is a method of programming supported by some languages (including Java) that focuses on constructs called "objects". An object consists of two things: some data (e.g. a list of names) and the operations which make use of or change that data (e.g. a routine to add a new name to the list, and another to sort the list). In OOP lingo, these operations of the object are called its methods.

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):

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 ; 
        }
}
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).

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.


Object-Oriented Programming in Java

The following commands are used in Java to create objects and to apply the methods for an object.

Creating Objects

variable = new class(constructor arguments);
To create a new instance of a class (i.e. a new object), use the new keyword, as in this example:
Student s;           // first declare a variable of the class Student
...                  // (...other code in between...)
s = new Student;   // later, create an instance of the class, referred to by s
Constructor arguments may be used to give information such as the size of an object or initial values, depending on how the class is defined. For example, an object of class String (character strings) can be initialized:
String msg;           // first declare a variable of the class String
...                  // (...other code in between...)
msg = new String("hello");   // later, create a String object, referred to by ms
g
                            // and initialized to "hello"

Accessing Objects

Once you have created an object, you can view and manipulate its data only by invoking the object's methods. The syntax for invoking a method is:
object.method(arguments)
where object is the name of the object instance, method is the name of the method to invoke, and arguments is a comma-separated list of values to be passed to the method. For methods that do not take any arguments, the arguments list is left blank (although the parentheses are still kept).

As an example, let's suppose we have defined a Student class that contains only one piece of data, a string to store the student's name. Also, suppose this class has only one method, called setName, that lets you change the name stored in the string. The code below illustrates how one would create a new student object and invoke its setName method:

Student s;             // declare a variable s of type Student
 
s = new Student;       // make a new Student object
s.setName("Allison");  // set the name field of this student to Allison


PREVIOUS 1 | 2 | 3 | 4 | 5 | 6 NEXT