CS 302 sect. 310
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 Overloading occurs when a child class redefines a method that would otherwise be inherited. False. This is overriding.
      2. T F Inheritance is the act of deriving a new class from an existing one. True.
      3. T F Inherited variables and methods can be used as if they were declared locally. True.
      4. T F All Java classes do not share a common base class. False. They are all subclasses of Object.
      5. T F Which version of an overriden method is used is determined by the type of the object, not the type of the reference. True. This is what polymorphism is all about.
      6. T F A parent's version of an overriden method can be called using the inherited keyword. False. The keyword in question is super.

    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 F can inherit:
        Don't forget that all classes inherit from Object! Also, in this case, D 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 + ":00"; }
      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 AlarmClock(8, 2);
      clockBox[count++] = new Clock(10);
      clockBox[count++] = new Clock(4);
      clockBox[count++] = new AlarmClock(6,6);
      for (int i = 0; i < count; i++) {
        clockBox[i].sound();
        System.out.println(clockBox[i]);
      }
      

      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:

      tick-tock
      8 o'clock, alarm set to 2 o'clock
      tick-tock
      10 o'clock
      tick-tock
      4 o'clock
      ring ring
      6 o'clock, alarm set to 6 o'clock