Abstract class vs. Interface


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Abstract class vs. Interface

  1. #1
    Join Date
    May 2004
    Posts
    242

    Abstract class vs. Interface

    Hello,

    I read about interfaces and it says :-

    ...
    An interface is not a class. When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes.
    ...

    But, isn't this what we do when we create abstract pure virtual class ? Can someone please tell me what is the difference ?

    Many thanks.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Essentially, an interface *is* an abstract class but there is a slight difference between the two terms. An abstract class is one that has at least one pure virtual function. You cannot instantiate such a class even if the rest of its member functions are fully defined:
    Code:
    class A
    {
    public:
     virtual ~A()=0; //pure virtual, hence the class is abstract
     virtual int f() { return 0;} //not pure, but still, A is abstract
    };
    By contrast, an interface defined all of its member functions as pure virtual. It has no data members and only public member functions. Often, it's a struct, not a class, to emphasize the fact that its member functions are all public:
    Code:
    struct File
    {
    virtual  ~File()=0;
    virtual int Open(int mode)=0;
    virtual int Close()=0;
    virtual int Delete()=0;
    virtual int Rename(const char*)=0;
    };
    An interface is not a C++ term. It's borrowed from other languages or design concepts. In C++, an interface means: the public members (data and functions) of the class.
    Last edited by Danny; 05-21-2010 at 10:45 AM.
    Danny Kalev

  3. #3
    Join Date
    May 2004
    Posts
    242
    Many thanks for that Danny.

    How would you go about data hiding in "interfaces" if they are all public ? (I don't mean the C++ interface as you refered to the data members and functions of a class, but similarly to one as you say borrowed from C# or Java) ?

    Also, in other words, is this type of design just like some sort of a template for other programmers to inherit from your 'struct' and implement each method later in the inheritance ?

    Thanks for your help.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    The interface is the user level, there is no data to hide (thats all inherited or whatever). Its just a wrapper to expose to the user the names of the objects that can be created and the methods that can be called by the user --- think of it as the wrapper you put around a library that you sell to someone to enable them to call what they need while everythign else in the system is hidden from them simply by not being included in the wrapper class/struct/whatever.

    It may or may not have virtual functions that the user can over-ride, some do and some do not. When you buy a library, most of the functions are not virtual, for example, they do the stuff that you spent good money to have done for you, it doesnt make sense to buy a bunch of empty functions that you write yourself... =)

  5. #5
    Join Date
    Oct 2007
    Posts
    369
    Danny,
    You have a parameter defined for your destructor. What does that mean?

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by hendrixj View Post
    Danny,
    You have a parameter defined for your destructor. What does that mean?
    Oops, just a typo. I had a constructor in mind...
    Danny Kalev

  7. #7
    Join Date
    Oct 2007
    Posts
    369
    This may or may not help:
    - In general inheritance of a base class (abstract or not) is used to implement a "isa" relationship: a square is a rectangle, a robin is a bird, etc.
    - Sometimes however, objects have similar behaviors without actual sharing a "isa" relationship. A pure virtual base class (called interface in some circles/languages) is an abstraction of what several non-related classes *do* as opposed to *are*.
    - Bot birds and airplanes *fly*, but they don't inherit from a common ancestor.

    Many objects need to save themselves to output streams and restore themselves from input streams. Sometimes this is described in a pure abstract class:
    Code:
    class Serializable
    {
        virtual bool serialize(std::ostream&)=0;
        virtual bool deserialize(std::istream&)=0;
    };
    Multiple inheritance of these "interfaces" is less error-prone (at least some would say) than multiple inheritance of meaty classes.
    There are plenty of practical and philosophical arguments on this topic.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by ami View Post
    Many thanks for that Danny.
    Also, in other words, is this type of design just like some sort of a template for other programmers to inherit from your 'struct' and implement each method later in the inheritance ?

    Thanks for your help.
    yes, that's exactly the point. Interfaces are a means of enforcing a certain set of services, without specifying the implementation. So you never have data members in interfaces -- data members usually belong to the implementation. Take for example the File interface. It's just a means of guaranteeing that everything that's called a file supports certain services, even if the file in question isn't a physical disk file.
    Danny Kalev

Similar Threads

  1. Help with Error in class
    By velkropie in forum Java
    Replies: 5
    Last Post: 03-24-2007, 12:09 PM
  2. Replies: 7
    Last Post: 01-16-2006, 05:04 PM
  3. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  4. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  5. Equivalent of VB6's Public Not Creatable Class
    By Wai-Yin Chee in forum .NET
    Replies: 8
    Last Post: 09-27-2001, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center