Implementing interface in the class hierarchy

Arek via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 14 04:04:29 PDT 2017


According to language reference (part 'Interfaces') this code 
will not compile:

interface D
{
     int foo();
}

class A : D
{
     int foo() { return 1; }
}

class B : A, D <- Error: class B interface function 'foo' is not 
implemented
{
}

Because: 'A reimplemented interface must implement all the 
interface functions, it does not inherit them from a super class'.

Why?

Each B object 'is an' A object (and each cat 'is an' animal) so 
if A implements D, then B implements D too. Implementing D second 
time doesn't change the nature of A and B.

More over, another example (more practical because here, the D 
interface is going to be implemented only once):

interface D
{
     int foo();
}

class A
{
     int foo() { return 1; }
}

class B : A, D <- Error: class B interface function 'foo' is not 
implemented
{
}

Class A doesn't implement D, but it has the method satisfied the 
D interface.

Why I have to provide the explicit implementation of 'foo' in B 
class?

I cannot logically explain this property of Dlang's OOP. Anyone 
could?

Thanks in advance.
Arek




More information about the Digitalmars-d-learn mailing list