what's different between interface and abstract class?

Allan QT chyi.tom at gmail.com
Fri Mar 16 00:59:15 PDT 2007


Thanks for your patient answers!
And at last I find their both definitions in digitalmars.com
As a return I would like conclude as following:

        1, interface: is like C++ abstract base class, but it's all member 
function must be pure virtual function.

        2, abstract: is an attribute that to instruct a class cannot be 
instantiate directly, but can be another class's base class.
                          beside this, the class with abstract attribute 
just like normal class.

            a class can be abstract:
                         1), declare it with abstract  attribute;
                         2), declare it's any member function(s) with 
abstract attribute.

Best regards.


"Jesse Phillips" <Jesse.K.Phillips+Digitalmars at gmail.com> 写入消息新闻:etdak9$22hi$1 at digitalmars.com...
> On Fri, 16 Mar 2007 15:22:54 +1100, Derek Parnell wrote:
>
>> On Fri, 16 Mar 2007 11:19:16 +0800, Allan QT wrote:
>>
>>> Thanks for your attention!
>>
>> I'm an amateur at this so someone please tweak/correct my offering.
>>
>> The differences include the fact that abstract classes can specify data
>> members and 'default' function bodies whereas interfaces cannot do that.
>> Also, the members of an interface *must* be implemented in the class that
>> is based on that interface but this is not a requirement for abstract
>> classes that have 'default' function bodies.
>>
>> I think that apart from those differences they are essentially the same
>> though I suspect they are implemented differently.
>>
>>
>
> Yes, the interface is just a way of saying that what implements me can do
> these things [...]. So there are data values, and only method signatures.
> An abstract class provides a base structure, but since the class is too
> "abstract" (not the entire concept) it can not have an instance.
>
> Abstract example:
> Classic example is the shape class, all shapes have at least one dimension
> (circle has a radius, square a length), color. All shapes can be drawn,
> but you can't draw a shape. You must know what the shape is before it can
> be drawn, and thus shape is an abstract concept.
>
> Interface example:
> Server interface, with this you could say that a server can wait for a
> connection, that it can receive and send a package. But depending on the
> server you don't know how or what connection it is waiting for, what will
> be in the packages, and you don't know what information the server will be
> holding. So all you can say about it is that a server:
>
> Socket connect(Socket foo);
> Package send();
> void receive(Package bar);
>
> Hope this helped to explain it.
> 




More information about the Digitalmars-d-learn mailing list