JMU
Lab: Questions About Abstract Classes and Interfaces


Instructions:Omitted

Getting Ready:Omitted

1. Basics: This part of the lab will help you determine whether you understand some of the basic issues involving abstract classes and interfaces.
  1. Familiarize yourself with the Comparator java.util.Comparator interface. (Note: For the time being, when you see a class with a one-letter name like T, you should assume that it is the Object class.)
  2. What methods are required of classes that implement the Comparator interface?

        public int compare(Object firstObject, Object secondObject)
        public boolean equals(Object other)
        

  3. The DirectoryComparator class will compile without error even though it doesn't implement the equals(Object) method required by the Comparator interface. Why?
    There are actually two reasons. First, it inherits the method from the Object class. However, even if it didn't, it would compile because it is an abstract class and, as such, is not required to implement all of its methods (even those it "promises" to implement).
  4. The DirectoryComparator class is abstract. What does that mean?
    You can't create instances of DirectoryComparator (i.e., you can't create objects that are members of the class DirectoryComparator).
  5. What method(s) in the DirectoryComparator class must be implemented by concrete children?
    The compare(File, File) method must be implemented. The equals(Object) method needn't be implemented because it is inherited from the Object class.
  6. DirectoryNameComparator extends DirectoryComparator. What method must it implement (in order to be concrete)?
    The compare(File, File) method (as discussed above).
  7. Does the DirectoryNameComparator implement the Comparator interface?
    It does, but "indirectly" since it's parent implements the interface.
  8. In the main() method of the DirectorySorter class, the variable comparatorToUse is declared to be a Comparator. Why is it possible to assign a DirectoryNameComparator or a DirectoryDateComparator to comparatorToUse?

    DirectoryNameComparator and DirectoryDateComparator implements the Comparator interface. Hence, for all intents and purposes, it behaves like it "is a" Comparator.

  9. When the DirectorySorter is executed with no command-line arguments, the compare(Object, Object) message is sent to comparatorToUse by Arrays.sort(). Trace the methods that are executed as a result of this message. [Note: For each step, list the signature of the method that is called and the class it is in.]

        1.  compare(Object, Object) in DirectoryComparator
        2.  compare(File, File) in DirectoryNameComparator
        

2. Design Issues: This part of the lab will help you determine whether you understand some of the issues involved in designing abstract classes and interfaces.
  1. What useful behavior/functionality do concrete children inherit from DirectoryComparator?
    The compare(Object, Object) method casts the parameters as File objects and calls the compare(File, File) method.
  2. Why does it make sense to have an abstract DirectoryComparator class? In other words, why shouldn't we just include the compare(Object, Object) method in the DirectoryDateComparator class?
    Because we might have other ways of comparing directories (e.g., based on size) that could use the compare(Object, Object) method in the DirectoryComparator class.
  3. The DirectorySorter class contains a containsSwitch method. Why must this method be static?

    It is called from the main() method which is static. (Since main() is static, it can be executed even if a DirectorySorter object does not exist. Hence, all methods and attributes it uses must also be static.)

  4. Draw a UML class diagram that shows the relationship between java.lang.Comparator, DirectoryComparator, DirectoryDateComparator, DirectoryNameComparator, and java.lang.Object. (NOTE: You do not have to submit your answer to this question.)

    overview.gif

3. More Design Issues: This part of the lab will help you determine whether you understand some of the issues involved in designing systems that have abstract classes and interfaces.
  1. Some of the classes and interfaces from previous labs are summarized in the following UML class diagram:

    AbstractClassesAndInterfaces.png

    Familiarize yourself with (or refresh your memory about) these classes and interfaces. (Note: In UML, abstract methods/classes are denoted by slanted text, specialization is denoted by an arrow with a solid line, and realization/implementation of an interface is denoted by an arrow with a dashed line.)

  2. Why is it a good idea to include the TwoPartMeasure class in this design? In other words, what would be bad about omitting the abstract TwoPartMeasure class?
    It implements methods that are needed in both of its subclasses, Length and Weight. If it were omitted, those two classes would contain duplicate code.
  3. Concrete subclasses of TwoPartMeasure must have an initializeUnits() method. What must be done in this method?
    The units for the small and large components must be initialized and smallsPerLarge must be initialized.
  4. Why is this abstract method included in TwoPartMeasure (rather than, for example, simply including a comment that says all subclasses must have such a method)?
    There are two reasons. First, by including it as an abstract method the compiler can check to ensure if the method has been included in concrete subclasses. Second, the initializeUnits() method is called in the TwoPartMeasure class' constructor.
  5. Syntactically, why must the TwoPartMeasure class be declared abstract?
    Because it has an abstract method.
  6. Conceptually, why should the TwoPartMeasure class be declared abstract?
    It doesn't make sense to have TwoPartMeasure objects. Length and Weight objects correspond to real-world entities/concepts but TwoPartMeasure objects do not.
  7. Why is it a good idea to include the Comparable interface in this design?
    It allows us to identify which objects can be compared and, hence, whether an array of a particular kind of objects can be sorted.
  8. In this design, how are TwoPartMeasure and HolidayAccount related?
    They are only related indirectly. That is, they are both Comparable.
  9. Why doesn't it make sense to have HolidayAccount extend TwoPartMeasure?
    Because there is no sense in which a HolidayAccount "is a" TwoPartMeasure. They have no attributes in common and the only method they have in common is compareTo() (and, perhaps, toString()).

Copyright 2011