Abstract Classes vs Interfaces
Robert Jacques
sandford at jhu.edu
Wed Jul 7 07:36:36 PDT 2010
On Wed, 07 Jul 2010 07:24:15 -0400, Justin Johansson <no at spam.com> wrote:
> Currently I'm struggling with unifying some C++ and Java code with
> the idea that I might eventually port it to D2.
>
> C++ people will know that their language supports abstract classes
> and multiple inheritance but not interfaces per se (although they
> can be hacked as abstract classes with implicit/trivial constructors).
>
> Java people will know that their language also supports abstract
> classes and interfaces though not multiple inheritance.
>
> In respect of the support of abstract classes, multiple inheritance
> and interfaces, both D1 and D2 are closer to Java than C++.
>
> May I please ask of this group their opinions as to the difference
> between abstract classes and interfaces from an axiomatic viewpoint.
>
> Is there a difference axiomatically, semantically or otherwise?
>
> Thanks in advance for all comments,
>
> Justin Johansson
Axiomatically, an interface can be thought of an abstract class without
any field or method definitions. In D2, interfaces now support contracts
and static/final methods. The real issues is that there are a host of well
known multiple-inheritance problems with abstract classes once you move
beyond virtual function (i.e. beyond interfaces). For example, It seems
that D2's interface functions have inherited some of these issues.
Consider the following:
interface A { final void foobar() { writeln("A"); } }
interface B { final void foobar() { writeln("B"); } }
class C : A, B {}
void main(string[] args) {
C c = new C;
c.foobar;
}
What does foobar print?
(This has been filed as bug 4435:
http://d.puremagic.com/issues/show_bug.cgi?id=4435)
More information about the Digitalmars-d
mailing list