Homework # 9
due Monday, April 6th, 11:00 AM

Covariance and Contravariance

Do Exercise 4 of Chapter 16 in the textbook (page 318), changed as follows:

Suppose we have a class $C_1$ with a method $m$ of type \( A_1 \to B_1 \). Suppose further that a derived class $C_2$ overrides this method with one of type \( A_2 \to B_2 \). Different languages have different rules about how the types $A_1$ and $A_2$, and $B_1$ and $B_2$ must be related. Investigate and report on this aspect of inheritance, citing the sources you used. Answer the following questions:
a.
Explain how this works in Java: what are the restrictions on $A_2$ and $B_2$ assuming $m$ is overridden.
b.
What is the rule called covariance? Explain the advantage of this rule. (Hint: see Eiffel)
c.
What is the rule called contravariance? Explain the advantage of this rule. (Hint: see Sather)
d.
What are parasitic methods? How do they address the problem of ``covariant overriding'' ?
e.
What are symmetric multimethods? How do they address the problem of ``covariant overriding'' ? What new problem do they cause?
Turn in your answers on paper.

Exception Handling

Write a program that does tests to see what happens for the 18 situations outlined in Exercise 4 on page 357. The program should have the following structure:

class ExceptionTest {
  static class Exception1 extends Exception { };
  static class Exception2 extends Exception { };
  static class Exception3 extends Exception { };
  static class Exception4 extends Exception { };
  private static String test(...) throws Exception {
     System.out.print("A");
     try {
       System.out.print("B");
       ... // do something: return "1", or throw Exception1 or 2
       System.out.print("C");
     } catch (Exception2 ex) {
       System.out.print("D");
       ... // do something: return "2", or throw Exception3
       System.out.print("E");
     } finally {
       System.out.print("F");
       ... // do something: return "3", or throw Exception4
       System.out.print("G");
     }
     System.out.print("H");
     return "4";
  }
  private static void dotest(String t, ...) {
    System.out.print(t + ". ");
    try {
      String s = test(...);
      System.out.println(" return " + s);
    } catch (Exception ex) {
      System.out.println(" throw " + ex);
    }
  }  
  public static void main(String[] args) {
    dotest("a",...);
    dotest("b",...);
    ...
    dotest("r"...); // 18 times
  }
}
Leave your code in the homework9 directory of your AFS volume.

Question: to be answered in written Homework:

There are four pairs of situations which have the same effect. What are these pairs? Explain why they have the same effect.
I found the ``sort -k 2'' Unix command to be very useful; ``man sort'' for more information

Submitting Your Work

Leave your code in the directory homework9 in your AFS folder: ExceptionTest.java. Turn in your written solution to Exercise 4 and the question to the Exception Test results on paper.


About this document



John Tang Boyland 2009-03-20