Lab Quiz Solution, Foundations of Computing, Spring, Week 6 -- Thurs May 10 2001

A file contains the following Java code (exactly as it appears here, there is nothing else in the file). The file is compiled by the Java compiler and the resulting Java program is run.

class Zeta {
    int i;
    public Zeta() { i = 0; }
    public void print_i() { System.out.print("  i " + i); }
}

class Sigma extends Zeta {
    int k; 
    public Sigma() { super(); k = 2; }
    public void print_k() { System.out.print("  k " + k); }
}

class Tau extends Sigma {
    int m;
    public Tau() { super(); m = 3; }
    public void print_m() { System.out.print("  m " + m); }
}

public class Inherit {
    public static void main(String argv[]) {
	Sigma s = new Sigma(); Tau t = new Tau();
	System.out.print("From s:"); s.print_i(); s.print_k(); 
	System.out.println();
	System.out.print("From t:"); t.print_i(); t.print_k(); t.print_m();
	System.out.println();
    }
}
  1. What is (are) the superclass(es) of Zeta?     Object

  2. What is (are) the subclass(es) of Zeta?     Sigma, also Tau (full credit for Sigma)

  3. What is (are) the superclass(es) of Sigma?     Zeta, also Object (full credit for Zeta)

  4. What is (are) the subclass(es) of Sigma?     Tau

  5. What is (are) the instance variables of Tau?     m, k, i

  6. What is (are) the instance methods of Tau?     print_m, print_k, print_i, also Tau, Sigma, Zeta (full credit just for the print methods)

  7. What class(es) do(es) the variable s belong to?     Sigma only, according to the language specification. Credit also given for the superclasses Zeta and Object (s is an instance of the superclasses, according to the specification.)

  8. What class(es) do(es) the variable t belong to?     strictly speaking Tau only, but credit also given for the superclasses Sigma, Zeta and Object

  9. What output does the program produce?
    From s:  i 0  k 2 
    From t:  i 0  k 2  m 3