Download Exercise on OOP Concepts: Multiple Inheritance, Dispatch, and Linearization and more Lecture notes Object Oriented Programming in PDF only on Docsity!
Concepts of Object-Oriented Programming
AS 2017
Exercise 6
Multiple Inheritance, Multiple Dispatch and Linearization
November 3, 2017
Task 1
(from a previous exam)
Consider the following C++ program:
class X { public : X( int p) : fx(p) {} int fx; }; class Y { public : Y( int p) : fy(p) {} int fy; }; class B : public virtual X, public Y { public : B( int p) : X(p-1),Y(p-2){} }; class C : public virtual X, public Y { public : C( int p) : X(p+1),Y(p+1){} }; class D : public B, public C { public : D( int p) : X(p-1), B(p-2), C(p+1){} };
int main() { D* d = new D(5); B* b = d; C* c = d; std::cout << b->fx << b->fy << c->fx << c->fy; return 0; }
What is the output of running the program?
(a) 5555
(b) 2177
(c) CORRECT: 4147
(d) 7177
(e) 7777
(f) None of the above
Task 2
Consider the following C++ code:
class Person { Person (^) *spouse; string name;
public : Person (string n) { name = n; spouse = nullptr ; }
bool marry (Person (^) *p) { if (p == this ) return false ; spouse = p; if (p) p->spouse = this ; return true ; }
Person (^) *getSpouse () { return spouse; } string getName () { return name; } };
The method marry is supposed to ensure that a person cannot marry him-/herself. Without
changing the code above, create a new object that belongs to a subclass of Person and marry
it with itself.
Hint: use multiple inheritance. Explain exactly what happens.
solution
The following C++ code breaks the invariant:
class B : public Person { public : B (string n) : Person (n) {} }; class C : public Person { public : C (string n) : Person (n) {} }; class D : public B, public C { public : D (string n) : B(n), C(n) {} };
void marryMyself () { D me ("Me"); B (^) *b = &me; C (^) *c = &me; b->marry (c); if (b->getSpouse ()) cout << b->getSpouse ()->getName (); }
The object me contains an object of class B and an object of class C. The addresses of these
objects are different and they are obtained using the assignments to b and c respectively.
During the call b->marry(c), the condition p == this compares these two addresses and
finds them not equal.
Task 3 (from a previous exam)
Consider the following Java classes:
add(s,m); add(s,s); }
public static Matrix add(Matrix m1, Matrix m2) { return m1.add(m2); } }
A) What is the output of this program? Please explain.
solution
The output is:
Matrix/Matrix
Matrix/Matrix
Matrix/Matrix
Matrix/Matrix
In each case, C# statically chooses a method variant based on the static type of the re-
ceiver. This is only add(Matrix other). Then at run-time, it will call the most-derived
override of this method. However, because C# method arguments are invariant, add(
SparseMatrix other) is not an override and we always end up calling the method from
Matrix.
B) Without breaking modularity, change only the body of MainClass.add to make it possible
to always call the most specific add method from the matrix hierarchy.
solution
We could change MainClass to the following:
public static Matrix add(Matrix m1, Matrix m2) { return (m1 as dynamic ).add(m2 as dynamic ); }
Now, the initial lookup for a method variant is also done at run-time, based not on the
static, but on the dynamic type of the receiver. Thus in the third and fourth case there
will be a choice between the two different add methods in class SparseMatrix. To also
enable a dynamic look-up of the most-specific method based on the argument types, we
additionally cast the argument as dynamic.
Task 5
Java 8 allows interface methods to have a default implementation directly in the interface.
A) What are some advantages of this feature?
solution
An advantage is obviously that default implementations can be reused in multiple classes.
Another advantage (and the main reason this feature is added to Java) is that default
method implementations will allow interface evolution. Without a default implementation,
adding new methods to an interface would break all existing classes that implement that
interface, since they do not contain an implementation for the new methods. The new
features removes this problem.
B) What could be some problems with this feature? How can they be resolved?
solution
A problem could be inheriting two default implementations of the same method from unre-
lated interfaces. In that case we will have to either choose which implementation we prefer
or write a new implementation that overrides both.
Another issue is that interfaces can now suffer from the fragile base class problem. Compared
to the usual issue with normal Java classes, this is even more dangerous for interfaces with
default methods, since these methods will mostly call other methods of the interface which
are overriden in implementing classes. A very restrictive solution here could be to prohibit
calls to other methods of the interface, within the implementation of default methods.
Alternatively we can “deal” with the problem just like Java deals with the issue in classes -
do nothing and rely on the programmer to be careful.
C) What problems of C++ multiple inheritance are avoided by this new design for Java
interfaces?
solution
We still avoid problems with correct initialization of fields of super types, since only one
super type (the extended class) can have fields, and we can directly call its constructor.
Furthermore there are no problems with field duplication as in non-virtual C++ inheritance.
D) Now suppose that, in addition to method implementations, Java also allowed interfaces
to define fields. Interfaces would not have constructors and interface fields would always be
initialized with a default value.
solution
This makes multiple inheritance in Java very similar to C++.
- What are some advantages of this feature?
solution
An advantage is that we can also reuse fields. This will enable more methods with
default implementations in interfaces which could increase code reuse and reduce the
effort required to create new classes.
- Given the restrictions above, are there any problems left with such an implementation of
multiple inheritance? If so what are they? Propose a solution for each problem you have
identified.
solution
These restrictions are somewhat similar to Scala traits, which also do not have spe-
cialized constructors (only a default constructor). In this way we manage to avoid
override def set(i: int ) = { super .set(i+1) } }
A) What is the difference between the following objects?
val a = new Cell val b = new Cell with Incrementing val c = new Cell with Incrementing with Doubling val d = new Cell with Doubling with Incrementing
solution
Object a behaves like a normal cell. Object b is also a cell, but it increases the stored value
by 1. The interesting difference is between c and d. They are both cells. They have mixed
in exactly the same traits. However, calling set(i) has a different effect on them: it stores
2i+1 to the first one and 2(i+1) to the second one.
B) We use the following code to implement a cell that stores the argument of the set method
multiplied by four:
val e = new Cell with Doubling with Doubling
Why doesn’t it work? What does it do? How can we make it work?
solution
Trait Doubling will not get mixed in twice, as perhaps the programmer would expect.
Scala rejects this statically.
The problem can be bypassed in an ugly way, by creating a new trait Doubling2 that
behaves exactly like Doubling and then introducing e = new Cell with Doubling
with Doubling2. Here is our first try:
trait Doubling2 extends Doubling val e = new Cell with Doubling with Doubling
The code passes through, but dynamically e behaves as if it were a Cell with Doubling.
Scala lets the code go through, because Doubling2 may introduce new functionalities, but
refuses to include Doubling twice in the linearization.
Our last try, the ugliest of all, but the one which will finally work, is to create a whole new
trait from scratch, reusing nothing:
trait Doubling3 extends Cell { override def set(i: int ) = { super .set(2*i) } } val e = new Cell with Doubling with Doubling
And now e.set quadruples its argument as expected.
C) Find a modularity problem in the above, or a similar, situation. Hint: a client that gets
given a class C does not necessarily know if a trait T has been mixed in that class.
solution
A problem is that a method that accepts Cell with Doubling with Incrementing as
an argument could also be passed a class of the type Cell with Incrementing with
Doubling - so what it can actually assume about its inputs is less than would be expected.
D) We propose the following solution to support traits together with behavioral subtyping:
Assume C is a class with specification S. Each time we create a new trait T that extends C, we
must ensure that C with T also satisfies S.
Show a counterexample that demonstrates that this approach does not work.
solution
Consider the following example:
class C { var x: int ; def foo() = {} //ensures true } trait T1 extends C { override foo() = { x=x+1 } //ensures x>old(x) } trait T2 extends C { override foo() = { x=x-1 } //ensures x<old(x) }
Both C with T1 and C with T2 are behavioral subtypes of C. But C with T1 with T
is not a behavioral subtype of C with T1.
Task 8
(from a previous exam)
Consider the following Scala code:
class A { def bar() = "" } trait B extends A { override def bar() = super .bar()+"B" } trait C extends B { override def bar() = super .bar()+"C" } trait D extends B { override def bar() = super .bar()+"D" }
object Main { def main() { foo( new A with D with C with B()) } def foo(x:A with D) { println(x.bar()) } }
What would be the output of the call Main.main()?
(a) BDB
(b) BBDBC
(c) BBCBD
virtual int get() { return B::get(); } };
C) What is the output resulting from the call new D(5) in method client?
solution
The code outputs “A15 B15 A25 C25 D5” (without whitespace).
D) Now, assume that MODIFIER is replaced by public virtual.
What is the new output resulting from the call new D(5) in method client?
solution
The code outputs “A1 B15 C25 D5” (without whitespace).