Next: None. Up: Primitive types. Previous: Type conversions.


Object types

Textbook: Section 3.1

We've seen this, but it's been causing problems, and after some deliberation, I thought of a new way to explain it. I fear it might confuse people more than it helps, but maybe it might help.

Long ago, philosophers were fascinated by words and what they meant. Plato had a philosophy that there were two realities: the tangible world, and the world of forms. A form is an abstract object that represents a category of objects. For example, there is a Human form, a category to which we all belong. For Plato, forms are not merely abstractions or categories, however: They are real - in fact, they are more real than tangible objects, since a tangible object may cease, but a form will never pass away.

For example, when we use a word, like chalk, we are referring to the abstract form Chalk. (I'm capitalizing it, because it is a name for a particular form. There's only one form for chalk, namely Chalk.) In our tangible world, there are many objects that participate in Chalkness - you can think of them as shadows of the Chalk form, projected into the tangible world where we can see and feel them. But there is only one Chalk form.

Java uses a slightly different terminology than Plato. Whereas Plato uses the word form, Java uses the word class. And whereas Plato might talk about concrete instances of a form to refer to objects in the tangible world, Java calls these instances or objects.

As Java programmers, we tell the computer about new forms using the Java language: That is, we define new classes. When we define a class like Ball, there is only one such class - just as there is only one form Chalk.

public class Chalk {
    // definition of Chalk form goes here
}

Properties

Notice that a particular form has certain properties. For example, all chalk is white (imagine that colored chalk was unfathomable); and, indeed, Plato would assert that the Chalk form is white. Such shared properties are class variables. (Calling it a variable is a slight misnomer - a platonic form is unchanging and permanent, so it's really a constant. Similarly, in Java, a class variable is usually declared final, making it too unchanging.)

On the other hand, tangible instances participating in the Chalk form - that is, individual pieces of chalk - have some properties too. Each individual piece of chalk has its own length, for example. Such a property is an instance variable.

public class Chalk {
    static final Color color = Color.white; // a class variable!

    double length; // an instance variable!

    // rest of definition comes here
}

Creating new instances

When a new piece of chalk is created, its instance properties will have fresh, new values. In Java, we define what these fresh values will be via what Java calls the constructor method.

    // part of the Chalk class definition: a constructor method
    Chalk() {
        length = 8.0; // 8 cm
    }
Now, whenever we generate a brand new piece of chalk, it creates a piece of chalk whose length property indicates that it is 8 cm long.

Behaviors

A form also has particular behaviors. These are called methods in Java. (Method wasn't a particularly good word choice, but we're stuck with it. If they used the word behavior, we'd confuse all those Canadians who can't spell properly.)

A form might itself do something. Plato would get upset about this - remember that a form is an unchanging, immortal entity. But we might ask the form something innocuous, like we might want a method so that we can ask if a piece of chalk would dissolve in some liquid liq. This is a property of the form Chalk, as it is an essential element to how chalk behaves. Thus it would be a class method. This particular class method would take a parameter (which we are calling liq for the purposes of defining the method) and return a boolean value.

    // part of the Chalk class definition: a class method
    static boolean willDissolve(Liquid liq) {
        if(liq.isAcid()) { // chalk dissolves in acid
            return true;
        } else {
            return false;
        }
    }

There are some things that only particular instances can do. For example, while the form Chalk cannot itself draw a line, we might ask a particular piece of chalk to draw a line. This would be an instance method. This particular instance method would take some parameters saying where to draw the line, and it wouldn't return anything.

    // part of the Chalk class definition: an instance method
    void draw(Graphics g, int x0, int y0, int x1, int y1) {
        if(length > 0.0) {
            g.setColor(color); // we're referring to our class variable
            g.drawLine(x0, y0, x1, y1);
            length -= 0.01; // chalk gets slightly shorter
        }
    }

Here's our complete definition of the Chalk form, with a couple of additional instance methods thrown in too.

public class Chalk {
    // class variable definitions
    static final Color color = Color.white;

    // class method definitions
    static boolean willDissolve(Liquid liq) {
        if(liq.isAcid()) { // chalk dissolves in acid
            return true;
        } else {
            return false;
        }
    }

    // instance variable definitions
    double length; // an instance variable!

    // constructor method definitions
    Chalk() {
        length = 8.0; // 8 cm
    }

    // instance method definitions
    void draw(Graphics g, int x0, int y0, int x1, int y1) {
        if(length > 0.0) {
            g.setColor(color); // we're referring to our class variable
            g.drawLine(x0, y0, x1, y1);
            length -= 0.01; // chalk gets slightly shorter
        }
    }

    int getLength() {
        return length;
    }

    Chalk split() {
        Chalk ret = new Chalk();
        length /= 2.0;
        ret.length = length;
        return ret;
    }
}


Next: None. Up: Primitive types. Previous: Type conversions.