Interface inheritance

Adam Burton adz21c at gmail.com
Sun Dec 12 04:16:33 PST 2010


Mandeep Singh Brar wrote:

> I just posted a bug on the bugs list about problems in compiling the
> following code using dmd, but realized that there might be some
> inheritance feature that i might now be aware of. Can you please let me
> know if i am missing something in the below code.
> 
> import std.stdio;
> interface A {
>         public void a(int l);
> }
> class ACl:A {
>         public void a(int l) {
>                 writeln("Hello a");
>         }
> }
> 
> interface B: A {
>         public void a(string l, int k);
> }
> 
> class BCl: ACl, B {
>         public void a(string l, int k) {
>                 writeln("Hello B.a", l, k);
>         }
> }
> 
> int main() {
>         B b = new BCl();
>         b.a(1);
>         return 0;
> }
> 
> However casting B to A like (cast(A)b).a(1); makes it work.
> 
> Mandeep
Try this

interface B : A
{
    alias A.a a;
    public void a(string l, int k);
}

I think this comes from hijacking. A derived type (B) provides a new 
overloads to a method in the base type (A). I think the idea is to avoid 
accidentally invoking base type overloads where they are not wanted so you 
have to explicitly let them through. For example if A.a was not there when 
you first created B, A being modified to include A.a will not affect code 
previously coded with B.a unless you explicitly let A.a in using alias. 
However once the alias is in it seems it is up to the programmer to enforce 
this rule if A is modified further.

http://www.digitalmars.com/d/2.0/hijack.html
http://www.digitalmars.com/d/archives/digitalmars/D/aliasing_base_methods_49572.html#N49577


More information about the Digitalmars-d-learn mailing list