References
References
References
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Objects and Classes<br />
Objects and classes are central concepts for Java programming. It will take you some<br />
time to master these concepts fully, but since every Java program uses at least a couple of<br />
objects and classes, it is a good idea to have a basic understanding of these concepts right<br />
away.<br />
An object is an entity that you can manipulate in your program, generally by calling<br />
methods. For example, System.out refers to an object, and you saw how to manipulate it<br />
by calling the println method. (Actually, several different methods are available: all<br />
called println, one for printing strings, one for printing integers, one for printing floatingpoint<br />
numbers, and so on.) When you call the println method, some activities occur<br />
inside the object, and the ultimate effect is that the object causes text to appear in the<br />
console window. For now, you should think of objects as a “black box” with a public<br />
interface – the methods you can call – and a hidden implementation – the code and data<br />
that are necessary to make these methods work.<br />
Different objects support different sets of methods. For example, the println method can<br />
be applied to the System.out object, but cannot be applied to the string object “Hello,<br />
World!”. That is, it would be an error to call<br />
“Hello, World!”.println(); // This method call is an error<br />
The reason is simple. The System.out and “Hello World!”<br />
objects belong to different classes. The System.out object is an object of the class<br />
PrintStream, and the “Hello World!” object is an object of the class String. You can apply<br />
the println method to any object of the PrintStream class, but the String class does not<br />
support the println method. The String class supports a good number of other methods;<br />
you will see many of them soon. For example, the length method counts the number of<br />
characters in a string. You can apply that method of type String. Thus,<br />
“Hello, World!”.length(); // This method call is OK<br />
is a correct call – it computes the number 13, the number of characters in the string object<br />
“Hello World!”. (The quotation marks are not counted.)<br />
A class has four purposes:<br />
1. A class specifies the methods that you can use for objects that belong to the class.<br />
2. A class is a factory for objects.<br />
3. A class is a holding place for static methods and objects.<br />
4. A class defines implementation details: the data layout of the objects and the code for<br />
the methods.<br />
In out first program, you saw the third (and least important) purpose. The Hello class<br />
holds the static main method. The System class holds the static out object.
To see how a class can be an object factory, let us turn to another class: the Rectangle<br />
class in the Java class library. Objects of type Rectangle describe rectangular shapes.<br />
Note that a Rectangle object isn’t a rectangular shape – it is a set of numbers that describe<br />
the rectangle. Each rectangle is described by the x- and y-coordinates of its top left<br />
corner, its width, and its height. To make a new rectangle, you need to specify these four<br />
values. For example, you can make a new rectangle with the top left corner at (5,10),<br />
width 20 and height 30 as follows:<br />
new Rectangle(5, 10, 20, 30)<br />
The new operator causes the creation of an object of type Rectangle. The process of<br />
creating a new object is called construction. The four values 5, 10, 20, 30 are called the<br />
construction parameters. Different classes will require different construction parameters.<br />
For example, to construct a Rectangle object, you supply four numbers that describe the<br />
position and the size of the rectangle. To construct a Car object, you might supply the<br />
model name and year.<br />
Actually, some classes let you construct objects in multiple ways. For example, you can<br />
also obtain a rectangle object by supplying no construction parameters at all (but you still<br />
must supply the parentheses):<br />
new Rectangle()<br />
This constructs a (rather useless) rectangle with the top left corner at the origin (0,0),<br />
width 0 and height 0. Construction without parameters is called default construction.<br />
To construct any object you do the following:<br />
1. Use the new operator<br />
2. Give the name of the class<br />
3. Supply construction parameters (if any) inside the parentheses<br />
What can you do with a Rectangle object? Not much, for now. Later will see how to<br />
display rectangles and other shapes in a window. You already know how to print a<br />
description of the rectangle object onto the console window – simply call the<br />
System.out.println method:<br />
System.out.println(new Rectangle(5, 10, 20, 30));<br />
The code prints the line<br />
java.awt.Rectangle[x=5,y=10,width=20,height=30]
Or more specifically, this code creates an object of type Rectangle, then passes that object<br />
to the println method, and finally forgets the object.<br />
Of course, usually you want to do something more to an object than just create it, print it<br />
and forget it. To remember an object, you need to hold it in an object variable. An<br />
object variable is a storage location that stores not the actual object, but information<br />
about the object’s location.<br />
You can create an object variable by giving the name of the class, followed by a name for<br />
the variable. For example,<br />
Rectangle cerealBox;<br />
This statement defines an object variable, cerealBox. The type of this variable is<br />
Rectangle. In Java, every object variable has a particular type. For example, after the<br />
cerealBox variable has been defined by the preceding statement, thereafter in the program<br />
it must always refer to an object of type Rectangle, never to an object of type Car or<br />
String.<br />
However, so far, the cerealBox variable doesn’t yet refer to any object at all. It is an<br />
uninitialized variable. To make cerealBox refer to an object, simply set it to another<br />
object reference. How do you get another object reference? The new operator returns a<br />
reference to a newly created object.<br />
Rectangle cerealBox = new Rectangle(5, 10, 20, 30);<br />
It is very important that you remember that the cerealBox variable does not contain the<br />
object. It refers to the object. You can have two object variables refer to the same<br />
object:<br />
Rectangle crispyCrunchyStuff = cerealBox;<br />
Now you can access the same Rectangle object both as cerealBox and as<br />
crispyCrunchyStuff.<br />
The Rectangle class has over 50 methods, some useful, some less so. To give you a<br />
flavor of manipulating Rectangle objects, let us look at a method of the Rectangle class.<br />
The translate method moves a rectangle by a certain distance in the x- and y-direction.<br />
For example,<br />
cerealBox.translate(15, 25);<br />
moves the rectangle by 15 units in the x-direction and 25 units in the direction and 25<br />
units in the y-direction. Moving a rectangle doesn’t change its width or height, but it<br />
changes the top left corner. For example, the code fragment<br />
Rectangle cerealBox = new Rectangle(5, 10, 20, 30);
prints<br />
cerealBox.translate(15, 25);<br />
System.out.println(cerealBox);<br />
java.awt.Retangle[x=20,y=35,width=20,height=30]<br />
Let’s turn this code fragment into a complete program. As with the Hello program, you<br />
need to carry out three steps:<br />
1. Invent a new class, say MoveRectangle<br />
2. Supply a main method<br />
3. Place instructions inside the main method<br />
However, for this program, there is an additional step that you need to carry out: You<br />
need to import Rectangle class from a package, which is a collection of classes with a<br />
related purpose. All classes in the standard library are contained in packages. For<br />
example, the System class and the String class are in a package called java.lang, and the<br />
Rectangle class belongs to the package java.awt. (The abbreviation awt stands for<br />
“Abstract Windowing Toolkit”). The java.awt package contains many classes for<br />
drawing windows and graphical shapes.<br />
To use the Rectangle class from the java.awt package, simply place the following line at<br />
the top of your program:<br />
import java.awt.Rectangle;<br />
You never need to import classes from the java.lang package. All classes from this<br />
package are automatically imported. For example, you can use the System and String<br />
classes without importing them.<br />
Thus the complete program is:<br />
Program: MoveRectangle.java<br />
import java.awt.Rectangle;<br />
public class MoveRectangle<br />
{<br />
public static void main(String[] args)<br />
{<br />
Rectangle cerealBox = new Rectangle(5, 10, 20, 30);<br />
cerealBox.translate(15, 25);<br />
System.out.println(cerealBox);<br />
}<br />
}