www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what's different between interface and abstract class?

reply "Allan QT" <chyi.tom gmail.com> writes:
Thanks for your attention! 
Mar 15 2007
next sibling parent Derek Parnell <derek nomail.afraid.org> writes:
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. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 16/03/2007 3:00:44 PM
Mar 15 2007
prev sibling next sibling parent reply Jesse Phillips <Jesse.K.Phillips+Digitalmars gmail.com> writes:
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.
Mar 15 2007
parent "Allan QT" <chyi.tom gmail.com> writes:
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 gmail.com>
写入消息新闻:etdak9$22hi$1 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.

Mar 16 2007
prev sibling next sibling parent Dejan Lekic <dejan.lekic gmail.com> writes:
Allan, think of interfaces as a "contract" betwen a programmer and user of the
class, which states that a certain class (which implements the interface) MUST
have certain methods declared in the interface.
Thus interface contains only SIGNATURES of methods that the class must
implement.

Class, however is, simply said, a template for making objects. It contains
members' and methods' definitions.
Mar 16 2007
prev sibling parent Knud Soerensen <4tuu4k002 sneakemail.com> writes:
On Fri, 16 Mar 2007 11:19:16 +0800, Allan QT wrote:

 Thanks for your attention!

I think that in practise the difference is that your get better data hidden and scalability using an interface than from an abstract class.
Mar 19 2007