Explore Interface Vs Abstract Class

Interface: An interface is very similar to a class but with some changes!! As we all know, classes are by default internal and interfaces are by default public. An interface can only have a declaration but not a definition. An interface can only have methods, properties, indexers and events whereas a class can have everything.
 
The main use of an interface is to support Multiple Inheritance and it removes the classic diamond problem that we encountered in C++.
 
Classic Diamond Problem
 
image1.jpg
 
In the preceding snapshot you can see that the D class objects will be confused about which method to call and raise an error in C++. Now we handle this problem with a C# interface but C# doesn't support Multiple Inheritance.
 
image2.jpg
 
image3.jpg
 
As in the preceding snapshots we can see that we got the desired result using of Multiple Inheritance but using an interface. As we mentioned above, there are two types of calling interface's method ; they are:
  1. Calling Implicit Interface Method
  2. Calling Explicit Interface Method
The Method defined without an Interface Name, i.e the way to calling an Implicit Interface method; a class can have only one Implicit Interface Method, as you can see in above snapshot, that First Display method is defined without interface Name and another is defined with the Interface name Ifc3.Display(), So this method definition tells to compiler that this Display() method is from Interface Ifc3, so its explicit calling of Interface Method and another one is implicit.
 
An interface called by an Interface Name is called an Explicit Interface; there can be many Explicit Interfaces in a class.
 
The following are some important things about interfaces:
  1. We cannot create an object of an interface, we can only create a reference or Interface Variable.
  2. An interface will have only abstract methods (method declaration).
  3. Interfaces supports Inheritance, Polymorphism (Overloading, Overriding (using the "new" keyword to hide the interface methods above)).
  4. We cannot create variables in an interface.
  5. Only Properties, Indexers, Methods and Events are allowed in an interface.
  6. A class can inherit multiple interfaces, which is also shown in the snapshot above.
  7. We cannot create any Access modifiers with Interface Members (like private, public, protected, internal, protected internal, virtual, override, static, abstract etc.)
  8. The new keyword is allowed in an interface.
  9. All methods of an interface must be defined in every derived class.
  10. An interface is good for small or medium level projects.
Abstract Class
 
Some points about an Abstract Class are:
  1. We cannot create an object of an Abstract Class.
  2. An Abstract Class can be inherited with its derived class.
  3. Can have both concrete and abstract methods but at least one abstract method is compulsory in an Abstract Class.
  4. An Abstract Class is used as a base class for projects.
  5. An Abstract Class can inherit another base class and base interfaces.
  6. We need to use the "abstract" keyword before defining the class.
  7. The constructor of an Abstract Class can be called through the derived class constructor.
  8. An Abstract Class should be used for a large project to share the common functionality with its derived class.
  9. All abstract methods must be implemented in its derived class, if inherited.
So now the question is when and where to use an interface and Abstract Classes.
 
In the following example of an Abstract Class, we can see that an Abstract Class with the name MotorVehicle is used. There are two methods, one is abstract and one is concrete, the abstract method is necessary for defining in its derived class. The concrete method Fuel() is the same for all the classes, as in the snapshot above.

Two derived classes are Car and Bike, both Car and Bike require fuel to run. That's why we use MotorVehicle as the base class and create a method Fuel, because it will not change for Car and Bike. Both needs Fuel; that's we create it as a base class method, and another method is an abstract method that we can change as we need in a derived class, as we used above.

image4.jpg
 
Now see what the output of the code above will be:
 
image5.jpg
 
This is the output of the above program, after seeing this I hope you guys can understand when to use an Abstract Class.

Interface

image6.jpg
 
In this example, we can see I created an interface IMobilePhone and two Classes A and B, Class A inherits the IMobilePhone Interface and implements all its methods. Class B also inherits the IMobilePhone Interface and here I didn't implement all the methods. Let's see what happens, so now I build the project.
 
image7.jpg
 
When I build the project, it gives me three errors indicating that Class B does not implement all its three methods DialNumber(), EndCall() and ReceiveCall(). So whenever you inherit an interface, you must implement all its methods, which is not required for an Abstract Class. So implement these methods and then check the output again.
 
image8.jpg

image9.jpg
 
image10.jpg
 
Now we can see that in the code above we implement all three methods of the interface and the run that. In the snapshot above we can see the output of the code.
 
And I hope you guys understand that before creating any project, first go through the requirements of your project and then choose which one is required for the project, interface or Abstract Class.
 
Try to use an Abstract Class as a base class for big projects where most of functionality is common in all the derived classes. Use an interface where, in every derived class, you want to define all the methods of the interface.
 
I hope this has helped all of you to understand the basic concepts of an interface.
 
Thanks in advance!!


Similar Articles