Abstract Classes vs Interfaces
Justin Johansson
no at spam.com
Thu Jul 8 06:04:49 PDT 2010
Ary Borenszweig wrote:
> On 07/07/2010 06:24 PM, Justin Johansson 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
First, thanks to all respondents.
> Basically an interface is a contract: if a class implement that
> interface then it provides all the methods described in the interface.
It's not that I don't understand interfaces and abstract classes; just
that I felt there was a underlying idea about some fundamental concept
that has hitherto been eluded. Now I think that there is nothing else
other than that the set of all abstract classes includes the set of all
interfaces and that about it. Abstract classes and interfaces are
simply variants of a tool for defining contractual call interfaces.
Different languages give you different features as to exactly what you
can do and cannot do with these tools.
> An abstract class just serves building common functionality for a set of
> classes that will inherit from it. If an abstract class provides no
> functionality at all then it might well be think of as an interface (it
> should be replaced by an interface.)
Good point you ended with there. Yes, I agree that the least featured
tool should be used when there is a choice of tools which are all
capable of doing the job, all other things being equal.
> From the other reply it seems that interfaces in D can have
> final/static methods. That kind of goes against what I just said about
> interfaces...
Yes, well, D provides implementation of these tools that might be
a little different to the concepts of these tools in other languages.
It's the abstract concept of abstract classes and interfaces which
was my main motivation in posting the question.
I think, though, from what you have said, abstract classes are
interfaces on steroids.
Worth mentioning though, that with the exception of multiple inheritance
alla C++), abstract classes in languages with single inheritance models
cannot declare classes which inherit from multiple abstract classes
Naturally, there's something tautological about this last point!
Cheers
Justin Johansson
More information about the Digitalmars-d
mailing list