Interface, Abstract Class, and Concrete Derived Classes
-
Interface
Is a C++ class with all pure virtual functions, an empty virtual destructor, and no data members. Its purpose is to establish a contract for services that can be implemented by any concrete derived class. -
Abstract class
Has at least one pure virtual function which prevents clients from creating instances. Abstract classes provide common code and sometimes common data, shared by every concrete derived class. Often an abstract class derives from an interface and defines some non-virtual functions to be shared. It may create instances of a common data type, shown as SharedResource in the Figure, above. If that type is qualified as static the instance is shared with all concrete derived classes. If not, then each concrete derived class gets a copy of the same type. Any pure virtual functions in its base must be defined by a concrete derived class. Otherwise the inherited function remains pure virtual and the derived class is also abstract. -
Concrete class
Must have definitions for all functions. It either inherits the definitions from a base or defines itself. The inherited definitions may be provided by any base class, e.g., the interface or the abstract base, shown in the diagram. When concrete derived classes are allocated on the heap it is crutial that they have a virtual destructor or the correct sequence of derived and base destructors will not get called on destruction. If the top-most base class has a destructor explicitly declared to be virtual, then all the destructors in the hierarchy will also be virtual, whether they are explicitly declared to be or not.
Conclusions for Distinguish between Interface, Abstract Class, &Concrete Class:
C++ classes in an inheritance hierarchy are either interfaces, abstract base classes, or concrete derived classes.
- Interface classes provide a contract for service without binding the client to an implementation.
- Abstract base classes support sharing of code and data resources across all concrete derived classes.
- Publically derived concrete classes are specializations of their base type(s), providing a service unique to that class. All of these specializations should implement the metaphor defined by their base. Think of public inheritance as an "is-a" relationship between derived and base classes.