CPE 102 Lab 4 - Interfaces, polymorphism, compile-time vs. run-time behavior, and run-time binding


You are going to read about some code that is fully described but not fully shown and determine if various code fragments would result in a compiler error, a run-time error (sometimes), or always compile and run correctly. Ideally, you will attempt to determine the correct answer simply by looking at the giving code then, after answering the questions you will implement the specified interfaces, classes, and develop tests for each of the fragments to determine the correct answer.

You are encouraged to use the specified code as a platform to explore more code fragments of your own design to increase your awareness of the issues and concepts explored by this lab.

What to Do:
  1. Read, carefully, this handout (you don't actually need to print it)
  2. After filling out (or just writing down) the table in the handout with your best answers:

    1. Write interfaces and classes that meet the descriptions found in the handout (A, B, C, D, and E). Be sure to include any specified methods in the correct interface or class.
    2. In a file called Tests.java, write tests for each code fragment to see if you answers were correct.

      1. Remember: You must determine how many different types of object references can be assigned to each reference type. When a reference can be assigned more that one type of object you will have to develop multiple tests for any code fragment that uses that reference to see if it compiles, always works, or only sometimes works. For example in the second row in the table, variable a could be referencing an object of type C (a = new C()) or D (a = new D()).
      2. Remember: Some will not compile. You will have to comment them out to continue your testing.

      3. Remember: Some will compile but fail at run-time. You will have to comment out the ones that fail at run-time to continue your testing.

      4. Remember: Some will always compile and run without error.

Lab courtesy of Kurt Mammen.