Review Questions

graphics/rq_icon.gif

6.27

Given the following code, which statements are true?

public interface HeavenlyBody { String describe(); }

class Star {
    String starName;
    public String describe() { return "star " + starName; }
}

class Planet extends Star {
    String name;
    public String describe() {
        return "planet " + name + " orbiting star " + starName;
    }
}

Select the two correct answers:

  1. The code will fail to compile.

  2. The use of inheritance is justified, since Planet is-a Star.

  3. The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class.

  4. The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class.

  5. An instance of Planet is a valid instance of HeavenlyBody.

6.28

Given the following code, which statement is true?

public interface HeavenlyBody { String describe(); }

class Star implements HeavenlyBody {
    String starName;
    public String describe() { return "star " + starName; }
}

class Planet {
    String name;
    Star orbiting;
    public String describe() {
        return "planet " + name + " orbiting " + orbiting.describe();
    }
}

Select the one correct answer:

  1. The code will fail to compile.

  2. The use of aggregation is justified, since Planet has-a Star.

  3. The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class.

  4. The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class.

  5. An instance of Planet is a valid instance of a HeavenlyBody.