To try and answer your original question more specifically. You have told the compiler that B derives from A inheriting method f. You have also said that B implements a virtual interface I. I does not have an implementation, despite sharing a common signature with f. If you just wanted f, there is no need to declare that it derives from I. If it needs to be both, but you want leverage A's implementation. Then this does the trick:

override int f(int x) { return super.f(x); }

providing an implementation for I wired to f. I think you are asking why doesn't the compiler assume I's implementation from A. Overriding a method in D is voluntary. The rationale is covered well in Andrei's book in section 6.4.4 if you have it. As you have declared a virtual method with the same signature, an implementation is required. As you have not provided one, by overriding the implementation found in A, the compiler complains.

----

It is not clear whether you are wanting to decorate existing implementations with interfaces or decompose a monolithic base class.

If the former, simply introduce all of the interfaces on the Base class. No wiring is then required to the subclasses. You get what you have now without the duplication and the subclasses can still vary the implementations if desired.

class Base : I0, I1, I2

As written, you are introducing numerous variants of the Base each with one additional interface defined. If you want to define several new types with subsets of Base functionality cleanly hidden behind the interfaces, the proxy pattern may serve your purpose better.

class Base {
   int m0() { return 0; }
   int m1() { return 1; }
   int m2() { return 2; }
}

interface I0 { int m0(); }
interface I1 { int m1(); }
interface I2 { int m1(); }

class A : I0 {
   private Base hidden = new Base();
   int m0() { return hidden.m0(); }
}

class B : I1 {
   private Base hidden = new Base();
   int m1() { return hidden.m1(); }
}

void main() {
   writeln(new A().m0());
   writeln(new A().m1());
}

If you inherited a 'god class', you will need to do some work to undo it.

Reply via email to