Using the classes you create in the Examples class

So far, we've been creating every class in (default package). I know I told some of you to import your classes in Examples.java (for example, import Tree;) This was wrong of me – you do not need to (in fact doing so is an error) import any class defined in (default package.) If you simply use your class in Examples without trying to import it, you should encounter no errors. See the screenshot below for an example.

Examples class using another class in (default package)

As you can see, in testHorse() I simply declare and initialize a horse (passing true to the constructor indicates that the horse does in fact have legs – since a legless horse wouldn't be very useful. Nor would he be very fortunate.) I didn't have to import Horse – and just to prove I'm not pulling any tricks, here's the code for the Horse class:

public class Horse {
    boolean _hasLegs;

    public Horse(boolean hasLegs) {
        _hasLegs = hasLegs;
    }

    public boolean hasLegs() {
        return _hasLegs;
    }
}

Note how the hasLegs() method simply returns the field _hasLegs. This type of method is called an accessor. It allows us access to the value of _hasLegs outside of the Horse object.