Abstract Class vs Interface - Real time scenarios to decide when to use abstract class and when to use interface.
The concept of Abstract classes and Interfaces is a bit confusing for beginners of Object Oriented programming. In this article I will describe what is the main difference between interface and abstract class? And what could be the real time scenarios to use abstract class and interface?
Abstract Class
Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'.
An abstract class can contain either abstract methods or non-abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
An abstract class does not mean that it should contain abstract members. Even we can have an abstract class only with non-abstract members.
abstract class absClass
{
public abstract void abstractMethod();
public void NonAbstractMethod()
{
Console.WriteLine("NonAbstract Method");
}
}
An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.
Interface
An interface defines a contract. An interface is not a class; it is an entity that is defined by the word Interface. The interface itself does not provide implementations for the members that it defines. An interface declaration may declare zero or more members. The members of an interface must be methods, properties, events, or indexers.
interface IInterface
{
public void Method1();
public void Method2();
}
The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
Difference between Abstract Class & Interface
-
Multiple Inheritances
- A class can inherit several interfaces.
- A class can inherit only one abstract class.
-
Default implementation
- An interface just defines a contract, it cannot provide any implementation.
- An abstract class can provide complete, default code and/or just the details that have to be overridden.
-
Access Modifiers
- An interface cannot have access modifiers for its members; all the members of interface are implicitly public.
- An abstract class can contain access modifiers for its members. But abstract members cannot have private access modifier.
-
Fields and Constants
- Fields and constants cannot be defined in interfaces.
- An abstract class can have fields and constants.
-
Constructor
- An interface cannot have constructor.
- An abstract class can have constructor.
-
Static Members
- Member of an interface cannot be static.
- Only Complete Member of abstract class can be Static.
-
Adding new functionality
- If we need to add a new method to an Interface then we will have to track down all the implementations of the interface and define implementation for the new method in all child classes.
- If we need to add a new method to an abstract class then we have the option of providing default implementation of that method in base class and therefore all the existing code might work properly.
-
Core VS Peripheral
- Interfaces are used to define the peripheral abilities of a class.
- An abstract class defines the core identity of a class.
Real time scenarios to decide when to use Abstract Class and when to use Interface
The MSDN states: "By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class."
Let me describe more scenarios in details here:
-
Homogeneity
If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behavior or status then abstract class is better to use. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
-
Size of Application
If you are designing small and short functionality then use interfaces. If you are designing large functional units, use an abstract class.
-
Future Expansion
If there is a chance of future expansion of your functionality/classes then abstract class is a good choice. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, should not be changed once created. If a new version of an interface is required, you must create a whole new interface.
On a different note, if there is any need, it is easy to add a new interface to the hierarchy. However, if you already have an abstract class in your hierarchy, you can't add another, i.e., you can only add an abstract class if there is not one available.
-
Objects Type
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
-
Decupling
You can use interfaces to decouple your application's code (Dependency Injection) from any particular implementation of it.