WRIGHT STATE UNIVERSITY
Department of Computer Science
and Engineering
CEG860 Winter
2007 Final Exam (30 pts)
Prasad
NOTE:
This is a Closed Book Exam. In your answers, be specific and to the point.
Incorrect claims will be penalized.
5.
Single and Multiple Inheritance (6 + 4 pts)
Explain
clearly the constraints on the object layout and the method table layout to
ensure code reuse (via inheritance) and dynamic binding (to accommodate
polymorphism with overriding), while implementing an object-oriented programming
language supporting single inheritance.
Explain clearly why this simple approach does not work for multiple inheritance.
class Super
{
void print() {
System.out.println( " \t Super.print().");
}
void rePrint() {
print();
System.out.println( " \t Super.rePrint().");
}
}
class Sub_i extends Super {
void print() {
System.out.println( " \t Sub.print().");
}
}
Carefully study the following attempted simulation of inheritance via
composition and
delegation, and explain clearly the fundamental differences between the two
versions of
Sub (Sub_i and
Sub_d) that prevent their exchange. Justify your claim by writing
code fragments and tracing it.
class Sub_d {
Super s = new Super();
void print() {
System.out.println( " \t Sub.print.");
}
void rePrint() {
s.rePrint();
}
}