Re: Adding abstract class...

From: christopher diggins (cdiggins_at_videotron.ca)
Date: 04/28/04


Date: Wed, 28 Apr 2004 07:37:39 -0400


"Robert C. Martin" <unclebob@objectmentor.com> wrote in message
news:m4ll801gti09jh4h9tgf3qrr5uon0h49io@4ax.com...
> On Sat, 24 Apr 2004 17:47:12 GMT, "bobsled" <sleding@sands.com> wrote:
>
> >Here's a post from Uncle Bob...
> >
> >"In C++ dependencies are very expensive, so isolation is that much more
> >important. Therefore I think it is often best to keep the interface
> >and add the abstract class below it. In Java or C# the dependencies
> >are not quite as expensive, and need for isolation not quite as great.
> >However, since abstract classes cannot be multiply inherited, it's
> >still probably best to keep the interface and add the abstract class
> >below it.
> >
> >On the other hand, if you started with an abstract class, and did not
> >have an interface; and if the system was working well without the
> >interface, I'm not sure I'd add it right away. I'd wait until some
> >event occurred that made the need for the interface obvious."
> >
> >I have some question and want to ask him:
> >
> >1. Is the difference between interface and abstract base class is the
former
> >consists only pure virtual functions but no member of data and no
> >non-pure-virtual function, and the latter has at least one pure virtual
> >function and either some members of data or non-pure-virtual function?
>
> Yes.

Strictly speaking this is not correct, an abstract base class by definition
doesn't have to have data members, or non-pure virtual functions. According
to the C++ Programming Language by Stroustrup Chapter 12.3: "A class with
one or more pure virtual functions is an abstract class, and no objects of
that abstract class can be created", further on he says also : "An abstract
class can be used only as an interface and as a base for other classes".

> >2. Is keeping adding such kind of abstraction into class hierarchy not
going
> >to gain performance penalty?
>
> No, there's no performance penalty. In C++ virtual deployment is very
> fast; and probably faster than the 'if' statement you'd need to
> achieve the same behavior without it.

It would be more correct to say there is a negligible performance penalty in
many cases. Adding abstract base classes into a hierarchy (compared to
correct implementation of interfaces) does have an execution time penalty
along with object size bloat which I demonstrate at
http://www.heron-language.com/article-cpp-interfaces.html which shows an
interface based design compared with an ABC that performs at 2x-4x as fast,
and reduces the size of the objects by half. The trade-off is that it uses
double width pointers as interface references. (note to Uncle Bob and those
familiar with my work: this links to my most recent work, which is IMHO more
convincing than my previous posts).

> >3. Is there any general rule for such kind of adding abstract class?
>
> There are many rules regarding inheritance, polymorphism, and abstract
> classes. See:
>
http://www.objectmentor.com/resources/listArticles?key=topic&topic=Design%20Principles

-- 
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com


Relevant Pages

  • Re: Declaring a Constructor in an Interface?
    ... i have read many places that an interface is faster than ... but would You also claim that an abstract class ... Dennis JD Myrén ... > Virtual methods always means performance overhead. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java/J2EE Openings in RTP, NC
    ... JSP, EJB, DAO Developer ... An abstract class is declared with the keyword "class" and is an implementation, i.e., its methods can contain bodies. ... An interface is a declaration of public method signatures which taken together represent a type with a defined contract for interaction with other types. ... A "Type 1" JDBC driver is a bridge to an ODBC driver which in turn interacts with the data store. ...
    (comp.lang.java.programmer)
  • Re: Abstract class or interface?
    ... >derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ... and rotate "things" along all three X, Y, and Z coordinate axis. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class or interface?
    ... I try to make the decision based on the relationship of the derived class to the base class. ... If the derived class "can act like" the type of the base class, I'd lean towards an interface. ... I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using abstract class that implements interface
    ... colleges - that come from JAVA programming - says - it is redundant ... and interface is unnecessary... ... abstract class is in whether all instances must be required to share the ... The design permits future implementations that do not share the same base ...
    (comp.lang.java.programmer)