Assignment 10
Due 4/13/04 9:30 a.m.
(Must be done with your group)

  1. While C++ supports multiple inheritance of classes, Java does not. 
    1. Does multiple inheritance add complexity to the language?  Explain and give examples to support your position.
    2. Is multiple inheritance harder to implement than single inheritance?  Explain and give examples to support your position.
    3. Do you think that multiple inheritance is useful to have?  Explain giving examples to support your position.
  2. Consider the following class hierarchy in which all methods are virtual (which is the default in Java):
    class A {
      void m() { print "A's m implementation"; }
    }
    class B extends A {
      void m() { print "B's m implementation"; }
      void n()  { print "B's n implementation"; }
    }
    class C extends B {
      void n() { print "C's n implementation"; }
    }
    i.e., C inherits from B and B inherits from A.  Also B overrides A's m method and C overrides B's n method. 
    1. What will be contents of the v-tables of objects of type A, B, and C?
    2. While referring to the contents of the v-tables, describe what happens in the following two  code fragments:
      /* fragment 1 */
      A a = new B();
      a.m();

      /* fragment 2 */
      A a = new B();
      a.n();
       
  3. Explain the benefits of including "interfaces" in Java.  Give examples to support your answer.
  4. Consider the following Java interface:
    interface I { void m(); void n(); }
    which doesn't inherit from any interface and has two methods m and n.  Now consider the following code that uses the interface:
    I i;
    i = ... /* initialized somehow */
    i.m();

    Will the correct target of the call "
    i.m()" always be at the same offset (0 for example) in the v-table of the object referenced by i? Why or why not?  Explain.
  5. Section 5.1.5 of the Java language definition (titled "Narrowing Reference Conversions") gives the narrowing conversions that Java allows.  Pick three of these conversions and for each of them give a successful example of the conversion (I say "successful" because a narrowing conversion may be successful or it may fail with a run-time error).