Chapter 6: Intro to OOP

OOPTest in Example 6.2 on pp. 147-8 is missing a closing brace. It should be

class OOPTest {

  public static void main(String args[]) {
    
    website w = new website();
    
    w.name = "Cafe Au Lait";
    w.url = "http://sunsite.unc.edu/javafaq/";
    w.description = "Really cool!";
    
    System.out.println(w.name + " at " + w.url + 
      " is " + w.description);
    
  }
  
}

David Hardy pointed out that the toString() method doesn't appear to actually be called in Program 6.11 as I claim on page 159. That program looks like this:

public class OOPTest {

    public static void main(String args[]) {
  
      website w = new website("Cafe Au Lait",
        "http://sunsite.unc.edu/javafaq/", 
        "Really cool!");
      System.out.println(w);
  
  }

}
Where's the call to toString()? It's inside the println() method.

The println() method calls the toString() method of any object which is passed to it. I should have been more explicit about this in the book. If an object doesn't have its own toString() method, then the nearest toString() method in its superclasses will be called instead. Typically this is the toString() method of java.lang.Object which merely prints the name of the class. Program 6.11 could have been written with an explicit call to toString() as shown below. However, the final result would be the same.

public class OOPTest {

    public static void main(String args[]) {
  
      website w = new website("Cafe Au Lait",
        "http://sunsite.unc.edu/javafaq/", 
        "Really cool!");
      System.out.println(w.toString());
  
  }

}
Exercise 1 on page 166 should read:

Convert the command line programs of exercises 1, 3, 4, 5, 6, 7, and 8 from chapter 4 into methods that do the calculations but don't handle any input. Then write a main method to handle the input and call the new methods. Do you notice anything similar about the new main methods?


Corrections to Other Chapters
[ Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]
Copyright 1996, 1998 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified September 12, 1998