|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
Following is the code for a class calledSimplePointthat represents a point in 2D space:This segment of code declares a class-- a new data type really-- calledpublic class SimplePoint { public int x = 0; public int y = 0; }SimplePoint. TheSimplePointclass contains two integer member variables,xandy. Thepublickeyword preceding the declaration forxandymeans that any other class can freely access these two members.You create an object from a class such as
SimplePointby instantiating the class. When you create a newSimplePointobject (we show you how shortly), space is allocated for the object and its membersxandy. In addition, thexandymembers inside the object are initialized to 0 because of the assignment statements in the declarations of these two members.Now, here's a class, SimpleRectangle, that represents a rectangle in 2D space:This segment of code declares a class (another data type)--public class SimpleRectangle { public int width = 0; public int height = 0; public SimplePoint origin = new SimplePoint(); }SimpleRectangle-- that contains two integer members,widthandheight.SimpleRectanglealso contains a third member,origin, whose data type isSimplePoint. Notice that the class nameSimplePointis used in a variable declaration as the variable's type. You can use the name of a class anywhere you can use the name of a primitive type.Just as
width"is an" integer andheight"is an" integer,origin"is a"SimplePoint. On the other hand, aSimpleRectangleobject "has a"SimplePoint. The distinction between "is a" and "has a" is critical because only an object that "is a"SimplePointcan be used where aSimplePointis called for.As with
SimplePoint, when you create a newSimpleRectangleobject, space is allocated for the object and its members, and the members are initialized according to their declarations. Interestingly, the initialization for theoriginmember creates aSimplePointobject with this code:new SimplePoint()as illustrated here:This diagram shows the difference between primitive types and reference types. Both
This figure has been reduced to fit on the page.
Click the image to view it at its natural size.widthandheightare integers and are fully contained withinSimpleRectangle. On the other hand,originsimply references aSimplePointobject somewhere else.The
SimplePointandSimpleRectangleclasses as shown are simplistic implementations for these classes. Both should provide a mechanism for initializing their members to values other than 0. Additionally,SimpleRectanglecould provide a method for computing its area. So, here's a new version ofSimplePoint, calledPoint, that contains a constructor which you can use to initialize a newPointto a value other than (0,0):Now, when you create apublic class Point { public int x = 0; public int y = 0; // a constructor! public Point(int x, int y) { this.x = x; this.y = y; } }Point, you can provide initial values for it like this:The values 44 and 78 are passed into the constructor and subsequently assigned to thenew Point(44, 78)xandymembers of the newPointobject as shown here:Now, let's beef up the SimpleRectangleclass. Here's a new version ofSimpleRectangle, calledRectangle, that contains four constructors, a method to "move" the rectangle, and a method to compute the area of the rectangle:The four constructors allow for different types of initialization. You can create a newpublic class Rectangle { public int width = 0; public int height = 0; public Point origin; // four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { this(new Point(0, 0), w, h); } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing the area of the rectangle public int area() { return width * height; } }Rectangleand let it provide default values for everything, or you can specify initial values for the origin, the width and the height, or for all three when you create the object. You'll see more of this version of theRectangleclass in the next section.This section glossed over some details and left some things unexplained, but it provides the basis you need to understand the rest of this lesson. After reading this section, you should know that
You also should have a general understanding or a feeling for the following:
- objects are created from classes
- an object's class is its type
- how "is a" differs from "has a"
- the difference between reference and primitive types.
Now, let's look in detail at the life cycle of an object, specifically how to create and use an object, and how the object finally gets cleaned up.
- How to create an object from a class
- What constructors are
- What the code for a class looks like
- What member variables are
- How to initialize objects
- What methods look like
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |