what's different between interface and abstract class?

Jesse Phillips Jesse.K.Phillips+Digitalmars at gmail.com
Thu Mar 15 22:40:25 PDT 2007


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