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:

class class_Abstarct
{
	public:
	virtual void pure_virtual_function() = 0;
	virtual void virtual_function()
	{
	cout << "user in virtual function" << endl;
	}
};

Interface: Class with all pure virtual functions

class class_Interface
{
	public:
	virtual void pure_virtual_function_1() = 0;
	virtual void pure_virtual_function_2() = 0;
};

Interface: Using __interface keyword

__interface class_Interface
{
	void pure_virtual_function_1() ;
	void pure_virtual_function_2() ;
};

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

class class_Interface
{
	public:
	virtual void show_class_name() = 0;
	virtual void show_developer_name(string data) =0;
};

class object_data: public class_Interface
{
	public:
	void show_class_name()
	{
		cout&lt;&lt;"class name : object_data"&lt;&lt;endl;
	}

	void show_developer_name(string data)
	{
		cout&lt;&lt;"developer name :"&lt;&lt;data&lt;&lt;endl;
	}
};

int main()
{
	class_Interface * data = new object_data;
	data-&gt;show_class_name();
	data-&gt;show_developer_name("XYZ");
	return 0;
}

INTERFACE (Using __interface keyword)

__interface class_Interface
{
	public:
	void show_class_name() ;
	void show_developer_name(string data) ;
};

class object_data: public class_Interface
{
	public:
	void show_class_name()
	{
		cout&lt;&lt;"class name : object_data"&lt;&lt;endl;
	}

	void show_developer_name(string data)
	{
		cout&lt;&lt;"developer name :"&lt;&lt;data&lt;&lt;endl;
	}
};

int main()
{
	class_Interface * data = new object_data;
	data-&gt;show_class_name();
	data-&gt;show_developer_name("XYZ");
return 0;
}

ABSTARCT CLASS

class class_Abstarct
{
	public:
	virtual void show_class_name() =0;

	virtual void show_developer_name()
	{
		cout &lt;&lt; "Developer name : XYZ" &lt;&lt; endl;
	}
};

class object_data: public class_Abstarct
{
	public:
	void show_class_name()
	{
		cout&lt;&lt;"class name : object_data"&lt;&lt;endl;
	}
};

int main()
{
	class_Abstarct * data = new object_data;
	data-&gt;show_class_name();
	data-&gt;show_developer_name();
	return 0;
}

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.

You may also like...

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

%d bloggers like this: