Program 7.10: The returnDelimitedWebsite Class

Suppose that one day you've just finished your website class. It's been plugged into your object oriented database which is chugging along merrily serving out files and responding to requests from the world. Then the the webmaster rolls in the door, and tells you that he needs a toString method that formats the website's data as three Strings on three lines like

Cafe Au Lait
Really cool
rather than
Cafe Au Lait at is Really cool!
What are you going to do? Your first reaction may be to change the class that you already wrote so that it formats String output in the requested form. However you're using that class elsewhere and things will break if you change it.

You could create a completely new class in a different file, either by starting from scratch or by copying and pasting. This would work, but it would mean that if you found a bug in the website class now you'd have to fix it in two files. And if you wanted to add new methods to the website class (as you will in the next few sections) you'd have to add them in two files. Still this is the best you could do if you were writing in C or some other traditional language. But you're not writing in C, are you? The OOP solution to this problem is to define a new class, call it returnDelimitedWebsite, which inherits from website and overrides website's toString method. You'll only need to write those parts of the class that have changed. Everything else can remain the same as in the website class and as bug free as that class is. If you do find and fix a bug in the website class, it will be fixed in the returnDelimitedWebsite class too. Here's the returnDelimitedWebsite class:

public class returnDelimitedWebsite extends website {

  public returnDelimitedWebsite(String n, String u, String d) {

      super(n, u , d); 


  public String toString() {
    return getName() + '\r' + getURL() + 
      '\r' + getDescription() + '\r';

The first thing to note about this class is what it doesn't have, getURL, getDescription, getName, setName, setURL, or setDescription methods or name, url and description member variables. All of these are provided by the superclass website. Nothing about them has changed so they don't need to be repeated here.

Next look at the toString method. This is different than the toString method in website. It formats return delimited lines of text rather than as a single line of text. Since the name, url and description member variables from website are not accessible to the subclass they're retrieved using the getName, getURL and getDescription methods. These methods are public so they are accessible to the returnDelimitedWebsite class.

The returnDelimitedWebsite constructor is a little more complicated. First note that if you're going to use a non-default constructor, that is a constructor with arguments, you do need to write a constructor for the subclass, even if it's just going to do the exact same thing as the matching constructor in the superclass. You cannot simply inherit website's constructor because that constructor is name website and this one must be named returnDelimitedWebsite. When a new returnDelimitedWebsite is created, the constructor that's looked for is returnDelimitedWebsite, not website.

The constructor needs to set the value of name, url, and description. However they're not accessible from the subclass. Instead they are set by calling the superclass's constructor using the new keyword super. When super is used as a method in the first non-blank line of a constructor, it stands for the constructor of this class's superclass, in this case website(String n, String u, String d).

The immediate superclass's constructor will be called in the first non-blank line of the subclass's constructor. If you don't call it explicitly, then Java will call it for you with no arguments. It's a compile time error if the immediate superclass doesn't have a constructor with no arguments and you don't call a different constructor in the first line of the subclass's constructor.

Copyright 1996 Elliotte Rusty Harold
This Chapter