CS 302 sect. 301
Quiz 6 (3% - 30 points)

Instructor: Dan Shiovitz

Name: _____________________________________________________________

  1. Terms and Concepts (6 points)

    1. [6 points total - 1 each] Is the statement below true or false? Circle one:
      1. T F All classes in Java are derived directly or indirectly from the Object class.
        True. Which is why they can all be stored in, for instance, Vector objects.
      2. T F Inheritance is the act of deriving a new class from an existing one.
        True.
      3. T F protected members are not visible outside the class and are not inherited.
        False. Not visible outside the class, correct. Not inherited, incorrect. private members are not inherited, but protected ones are.
      4. T F Overloading occurs when methods have the same name but different signatures.
        True.
      5. T F Which version of an overriden method is used is determined by the type of the reference, not the "actual" type of the object.
        False. Polymorphism means we figure out the type of the object and call the right method, regardless of the reference type (for instance, the AlarmClock objects still go ring-ring even when stored in an array of type Clock)
      6. T F super can be used to call the parent's constructor.
        True.

    2. [4 points total] Fill in the questions below based on the class hierarchy provided.
      The hierarchy, you remember, looks like:
      
               A
             / | \
            B  C   D
            |      |
            E      F
         

      1. [1 point] How many classes are derived from class A?
        3 directly, 2 more indirectly.

      2. [1 point] In which class should features common to all of the classes be located?
        A of course.

      3. [2 points] List all of the classes from which class E can inherit:
        Don't forget that all classes inherit from Object! Also, in this case, B and A.

  2. Coding Inheritance [20 points]

    Consider the following class definition:

    class Clock {
      protected int currentHour;
    
      public        Clock (int h) { currentHour = h; }
      public String toString()    { return currentHour + " o'clock"; }
      public void   sound()       { System.out.println("tick-tock"); }
    }
    
    1. [10 points] Derive a subclass from Clock named AlarmClock, which should have the following:
      1. an integer for the hour when the alarm triggers
      2. a sound of "ring ring" when the current hour equals the alarm hour, otherwise the sound is "tick-tock" as before
      3. a toString method that displays both the current hour and the alarm hour
      4. a constructor taking two integers that are used to initialize the current hour and the alarm hour. The constructor must correctly make use of its parent's constructor.

      Reasonably straightforward, I hope:

      class AlarmClock extends Clock
      {
        private int alarmHour;
      
        public AlarmClock(int h, int ah) 
        { 
          super(h); 
          alarmHour = ah; 
        }
        public String toString()
        { 
          return super.toString() + ", alarm set to " + alarmHour + " o'clock";
        }
        public void sound()
        {
          if (currentHour == alarmHour) System.out.println("ring ring");
          else                          super.sound();
        }
      }
      

    2. [10 points] Given the two class definitions (you may assume your class in part (a) works correctly as described), show the output of the following code:

      Clock[] clockBox = new Clock[8];
      int count = 0;
      clockBox[count++] = new Clock(7);
      clockBox[count++] = new AlarmClock(3, 6);
      clockBox[count++] = new Clock(3);
      clockBox[count++] = new AlarmClock(12,12);
      for (int i = 0; i < count; i++) {
        System.out.println(clockBox[i]);
        clockBox[i].sound();
      }
      

      The important point here is that even though the array is of Clock objects, Java correctly figures out if it's an AlarmClock object or a Clock object and calls the appropriate method. Also, remember count++ evaluates to count's old value, not its new one. Anyway, the output is:

      7 o'clock
      tick-tock
      3 o'clock, alarm set to 6 o'clock
      tick-tock
      3 o'clock
      tick-tock
      12 o'clock, alarm set to 12 o'clock
      ring ring