JMU JMU - Department of Computer Science
Lab: Experimenting with Abstract Classes

Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Basics: This part of the lab will help you understand the basics of abstract classes.
  1. Open TwoPartMeasure.java.
  2. Compile TwoPartMeasure.java to ensure that it has no syntax errors.
  3. Remove the abstract modifier from the declaration of the TwoPartMeasure class and the declaration of the initializeUnits method.
  4. Compile the TwoPartMeasure class.
  5. What error was generated?
  6. Replace the two abstract modifiers that you removed in step 3.
  7. Compile the TwoPartMeasure class.
  8. Why does this class compile even though it has a method with a missing method body?
  9. "Comment out" the initializeUnits method.
  10. Compile the TwoPartMeasure class.
  11. What error was generated?
  12. "Un-comment out" the initializeUnits method.
  13. Open Driver0.java.
  14. Compile Driver0.java.
  15. What error was generated and why?
  16. Why doesn't the declaration of the measure variable generate a compile-time error?
2. Specializing an Abstract Class: This part of the lab will help you understand extensions/specializations of abstract classes.
  1. Open Length.java.
  2. Is this class "concrete"? Why or why not?
  3. Open Driver1.java.
  4. Compile and execute Driver1.
  5. What output was generated?
  6. Is this output correct?
  7. "Comment out" the implementation of the initializeUnits() in the Length class.
  8. Compile the Length class.
  9. What error was generated?
  10. "Un-comment out" the implementation of the initializeUnits() in the Length class.
  11. Create a Weight class (containing pounds and ounces) that extends the TwoPartMeasure class.
  12. What code did you have in your implementation?
3. Type Safety and Abstract Classes: This part of the lab will help you understand "type safety" issues that sometimes arise with specializations of abstract classes.
  1. Open Driver2.java.
  2. Compile the Driver2 class.
  3. Why doesn't the expression myLength.equals(myWeight) generate a compile-time error?
  4. Execute Driver2.
  5. What output is generated?
  6. Why does the weight equal the length in the example above? (Note: We will see how this "problem" can be fixed later in the semester.)
4. Using Abstract (and Concrete) Classes: This part of the lab will help you understand some of the ways in which abstract classes and their concrete children are used.
  1. Open LengthDatabase.java and DatabaseDriver1.java.
  2. Compile and execute DatabaseDriver1.
  3. Did it execute correctly?
  4. Create a file named DatabaseDriver2.java that creates and store Weight objects in the LengthDatabase rather than Length objects.
  5. Compile your implementation of DatabaseDriver2.
  6. Why didn't it compile?
  7. Open MeasurementDatabase.
  8. Modify DatabaseDriver1 and DatabaseDriver2 so that the database they use is a MeasurementDatabase. Make sure you change both the declaration and the instantiation.
  9. Compile DatabaseDriver1.
  10. What error was generated?
  11. Correct this error (and the corresponding error in DatabaseDriver2 by casting the value returned by database.get(name) to the appropriate type.
  12. What changes did you make?

Copyright 2011