Assignment 9
Due 11/09/04 12:30 p.m.
(Must be done with your group)
- [synthesis] The static and dynamic links for an activation record may
both point to the same activation record or to different activation records.
- Give an example where the static and dynamic links of an activation
record, A, both point to an activation record B. Explain your example.
- Give an example when the static and dynamic links of an activation
record, A, point to different activation records. Explain your example.
- Consider routines, A, An1, An2, and B. Assume that An1 and An2 are
immediately nested inside A, and A and B are immediately nested inside main.
- [skill 16.2] Main calls A, which calls An1, which calls An2, which
calls B. Show (using ascii art) or describe what the stack looks like
when B is called.
- [skill 16.3] Assuming static scoping, describe how An2 accesses a
variable declared inside A. Be clear about how you will traverse static
and/or dynamic links to access the variable.
- [synthesis] While C++ supports multiple inheritance of classes, Java does not.
- Does multiple inheritance add complexity to the language? Explain
and give examples to support your position.
- Do you think that multiple inheritance is useful to have? Explain
giving examples to support your position.
- [skill 17.3] 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 the output of the following code fragment. Explain your
answer.
/* fragment 1 */
A a = new B();
a.m();
/* fragment 2 */
A a = new B();
a.n();
- [skills 17.1, 17.2] Come up with a real example where you
use inheritance and dynamic dispatching. Your example should be based on
a real problem and not contrived. Write the entire program in C++ or
Java. Your code should compile and run.
- [skill 18.1] Explain the benefits of including interface types in Java. Give
one example to support your answer.
- [skill 18.2] 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).
- [skill 18.2] Consider the following code fragment:
interface I {
void f();
}
class A implements I {
void f() { print "A's f implementation"; }}
}
class B implements I {
void f() {print "B's f implementation"; }}
- What are the subtyping relationships between A, B, and I.
- Explain why the following code is legal and what it will print.
Assume that b is a boolean variable declared and initialized elsewhere.
{
I i;
if (b) {
i = new A();
} else {
i = new B();
}
i.f();
}