8. Using pre-defined objects
- Introduction to a few simple objects and their usage
- Textfields, Textareas, Labels, Buttons, Canvases, Checkboxes
- Example: Objects laid out in an applet; familiarize usage of methods
- Methods vs. Functions
8.2.1 Introduction to a few simple objects and their usage
For our purposes, we will think of an object as some graphical component: A Button is an object, a Textfield is an object. We are going to play around with these objects in order to understand them better. Each one of these components has certain properties that we can use, read, and modify. Such a property is, for example, the color of the object, or the text that appears on the object. A button would be useless without some kind of text or an image that describes the action of the button. Such are the properties we can change and use.
Properties, as we have just referred to, can be accessed via an entity called a
method. These methods are embedded in the objects and provide us with the oportunity to do certain things with the objects. These methods are no different from functions in what they look like. Methods have the property of being very descriptive of what they intend to do. For instance, a method that gets something from an object is most likly going to be named getSOMETHING(), where SOMETHING is named after the something that the function returns. Similarly, setSOMETHING() assignes something to the object. isSOMETHING() examines the state of the object. We cannot simply call getMyName() from an object, because every one of these methods must be actually defined in the object. If getMyName() is not defined, then we cannot use it. In this case, the compiler will output some type of error message, warning us that we are using something that does not exist.
Let us look at an undefined, hypothetical object, called a
widget. This widget happens to have some text associated to it. It doesn't matter right now, where the text appears or what purpose the widget serves. Let us assume that the text in the widget can be retrieved by a method called getText(). You may see the resemblance of this method with a function, as the method has two paretheses with no arguments. In order to call this method from a widget, we would use:
Let us say that another method is called setText(String text), which replaces the text in the widget with another piece of text. Note how this method takes an argument by the type of String. This is the text used to replace the existing text in the widget. We would use it as follows:
widget.setText("This is my new text");
8.2.2 Textfields, Textareas, Labels, Buttons, Canvases, Checkboxes
Use the following tool to experiment with methods and objects. It contains some commonly used components, such as Textares, Textfields, Buttons, Labels, and Checkboxes. In the demo part of the tool, you may try out some of the methods that exist for each of the components.
After you have gained some insight into the workings of objects and methods, use the following source file to write some methods that you saw in the tool in actual code. For that you may want to pay careful attention to the usage field at the bottom of the tool.
8.2.3 Methods vs. Functions
In procedural programming, we make use of functions in order to manipulate data. There are no such thing as explicit objects, like the Buttons or Textareas that we used before. Instead these objects are represented as implicit objects, that is, we can think of them as objects, but the programming language does not supprot such a thing as an explicit object. In OOP, objects now contain data as well as the functionality to manipulate the data. This functionality comes in the form of methods. They are conceptually not too different from functions, yet they denote that they are intrinsic to an object. By using widget.getText(), we are calling up some functionality from within widget, which may not be present in some other object called appletree. The equivalent function to this method in a non-OOP environment, could, for example, be still called getText, yet it may take an integer as an argument, which points to the type of component, from which the text is to be retrieved. The difference is subtle, yet good to know.