Assignment 11, Implementation of Object-Oriented Languages and Exception
Handling
Due December 4th, 12 noon
(To be done with your group)
- 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.
- What will be contents of the v-tables of objects of type
A, B, and
C?
- 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();
- 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.
- Explain why multiple inheritance is more
complicated to implement than single inheritance. Give an example to
support your answer.
- 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).
- Page 577, Problem set, Problem 9 (ps: the
question says "...following Java skeletal program" but the program really uses
C++ syntax).
- Give an example that illustrates the usefulness of the "finally" part of
Java's try-catch-finally statement. Explain your example.