C++ – Different between Abstract Class and Interface

In this article we will discuss about the difference between Abstract class and Interface.

Abstract Class:

An abstract class is, conceptually, a class which contain at least one pure virtual function. It can’t be instantiated as it contains pure virtual function, but it can have implementation in it.

Interface:

Interface is a class which contains a virtual destructor and only pure virtual functions.

It can’t be instantiated as it contains pure virtual function, and also it can’t have any implementation in it.

To create interface, C++ also provide a keyword __interface.

__interface have different semantics (mentioned below) even though it is equivalent to class with all pure virtual functions.

Any method declared will be pure virtual even if programmer don’t specify “virtual” and “=0”.

It can’t contain any private members or any private data members.

Note *The interface keyword is basically used just to make it easier to read.

E.g.:

Abstract class:

Interface: Class with all pure virtual functions

Interface: Using __interface keyword

Uses:

Abstract class: As mentioned, it can have implementation in it. So it is used as a base class for strongly related classes. So that it can have implementation for common functionalities and provides the flexibility to the derived class whether to override the implementation or not.

Interface: As mentioned, it can’t have any implementation in it. So generally it is used as a base class of unrelated classes. So the derived classes must have to give the implementations. The Interface is mostly used in layer architecture projects because of it provides light coupling between layers. So it helps in reducing the compilation time (if modification is done in one layer, only that layer needs to be compiled instead of compiling entire project).

Differences:

Abstract class Interface
An abstract class can have data members and/or implementation for some of its member functions. An Interface can have only static constant data members, which are compile time constant and can’t have any implementation.
A derived class have the flexibility to provide the implementation. A derived class must have to provide the implementations for all the members of the Interface.
An abstract class can inherit from another abstract class and or from another Interface.  An Interface can inherit from another Interface only and can’t inherit from an abstract class.
If coder add any new method to an abstract class, coder has the option of providing a default implementation for that method. So the existing code will continue to work without any changes. If coder add any new method to an Interface, all the derived class must have to provide the implementation for that method.

Demo:

INTERFACE

INTERFACE (Using __interface keyword)

ABSTARCT CLASS

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.