<< Chapter < Page | Chapter >> Page > |
Custom class definitions
In almost all cases, you will need to define a few new classes for new applications that you write. We will define two new classes for this application. The remainder of the classes that we use will come either from Oracle's standardlibrary or Ericson's library.
Objects of the World class and the Turtle class
Ericson's class library contains a class named World and another class named Turtle . The code in Listing 2 instantiates one object of the World class and populates that world with two objects of the Turtle class.
Every class has a constructor
Every class definition has one or more method-like members called constructors. (If you don't define a constructor when you define a class, a default constructor will be automatically defined for your class.)
The name of the constructor must always be the same as the name of the class. Like a method, a constructor may or may not take arguments. If there are two or more (overloaded) constructors, they must have different argument lists.
Instantiating an object of a class
To instantiate an object of a class, you apply the new operator (see Listing 2 ) to the class' constructor, passing parameters that satisfy the required arguments for the constructor.
Return a reference to the object
Once the object has been instantiated, the constructor returns a reference to the new object.
A new World object
For example, the first statement in Listing 2 applies the new operator to Ericson's World class constructor passing two integer values as parameters. This causes a new World object to be instantiated.
A reference is returned
A reference to the new World object is returned and stored in the reference variable named mars .
Once the reference is stored in the reference variable, it can be used to access the World object later.
Constructors for the World class
Figure 4 shows the overloaded constructors that are available for Ericson's World class. (See javadocs for the Ericson library.)
Figure 4 - Constructors for the World class.
A new World object
The third constructor in Figure 4 was used to construct a World object in Listing 2 with a width of 300 pixels and a height of 274 pixels. As explained earlier, this object's reference was saved in the variable named mars .
Two new Turtle objects
The last two statements in Listing 2 instantiate two objects of the Turtle class and use them to populate the World object whose reference is stored in the variable named mars .
More complicated than before
This is a little more complicated than the instantiation of the World object. Ericson's javadocs indicate that the Turtle class provides the four overloaded constructors shown in Figure 5 .
Figure 5 - Constructors for the Turtle class.
A World object as a parameter
If you dig deep enough, and if you study Ericson's textbook, you can determine that the third constructor in Figure 5 will accept a reference to a World object as a parameter. This is the constructor that was used in the last two statements in Listing 2 .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?