Tag Archives: interfaces

Abstract class versus Interface

  • Interface means a C++ class with only pure virtual methods (i.e. without any code), instead abstract class  means a C++ class with virtual methods that can be overridden, and some code, but at least one pure virtual method that makes the class not instantiable
  • Interfaces have all the methods having only declaration but no definition.
  • In an abstract class, we can have some concrete methods. In an interface class, all the methods are public. An abstract class may have private methods.

The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.

Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.

Classes that can be used to instantiate objects are called concrete classes.

An object-oriented system may use an abstract base class to provide a common and standardized interface appropriate for all the external applications. Then, through inheritance from that abstract base class, derived classes are formed that all operate similarly.

The capabilities (i.e., the public functions) offered by the external applications are provided as pure virtual functions in the abstract base class. The implementations of these pure virtual functions are provided in the derived classes that correspond to the specific types of the application.

This architecture also allows new applications to be added to a system easily, even after the system has been defined.

Q. How do dynamic linking works ? what are vtables ?