Difference Between Interface vs. Abstract Class in .Net Core
We know that Microsoft is always introducing new features in every released version of C#. As of today (Dec 2013), C# 12 is the latest version which was introduced with .NET 8 in Visual Studio 2022. So there are a lot of new features are introduced for the interfaces as well. Now the question has been raised "Is it true that Interfaces has replaced the Abstract Classes in .Net now?".
I have seen that currently both interviewer and interviewee are still referring to the old traditional interface vs. abstract class answers, which is not correct nowadays. So let's clarify all the doughts today.
Contents of the Day
Q01. What is the use of Abstract Class and Interface in .Net?
Q02. What is Abstract Class in .Net?
Q03. What is Interface in .Net?
Q04. What are the current fundamental rules of using Abstract Class in .Net?
Q05. What are the things that are allowed in an Abstract Class?
Q06. What are the things that are not allowed in an Abstract Class?
Q07. What is the difference between the traditional Interface and the current .Net Core Interface?
Q08. What are the new features allowed in the current .Net Core Interface??
Q09. What are the features that are not allowed in the .Net Core Interface?
Q10. What are the similarities between Abstract Class and C# 8 Interface?
Q11. What is the difference between Abstract Class and Interface?
Q12. If both Abstract Classes and Interfaces are used for abstraction, what distinguishes them?
Q13. What are the recent features introduced for the interface in .Net?
Introduction
The story begins with C# 8 when Microsoft has introduced a new era of interface. Actually, C# 8 was introduced in .Net Core 3.x with Visual Studio 2019 which follows and implements .Net Standard 2.1. You can check and change your C# version by following this link and the .Net Framework version by following this link.
Therefore I will use the term "Traditional Interface" or ".Net Interface" for the interface supported in traditional .Net Framework (before C# 8) on the other hand the term "new interface" or ".Net Core Interface" for the interface after .Net Core 3.x (after C# 8). I am sure at the end of this article you will be surprised: "Why interfaces are not abstract classes?". But before that let's see some basic terminologies like data abstraction, abstract class, and interface.
What is Data Abstraction? How data abstraction can be achieved in .Net?
Data Abstraction is the process of hiding certain details and showing only essential information to the user. The data abstraction can only be achieved with either abstract classes or interfaces.
The abstract modifier indicates incomplete (missing) implementation or partial implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. An interface provides the contract but not the operation; on the other hand, an abstract class provides the execution, the data, and functions that must be adapted.
What is Abstract Class in C#?
An abstract class is half defined or partially defined class that cannot be instantiated. An abstract class can be declared with the abstract keyword and the abstract modifier indicates incomplete implementation or partial implementation. If the abstract modifier is used in a class declaration, it indicates that a class is intended only to be a base class of other classes, not instantiated on its own.
Similarly, you can use the abstract modifier in a method or property declaration to indicate that the method or property does not contain any implementation. All the abstract members of the class must be implemented by the derived non-abstract classes. Please read more about the abstract classes here.
What are the current fundamental rules of using Abstract Class in .Net?
- A class containing abstract members must be abstract.
- All the derived classes must override all the abstract members with the override keyword explicitly.
- All the concrete classes derived from the abstract class must provide the actual implementations of all inherited abstract members.
- The name of an abstract class must be a valid C# identifier name.
- The keyword abstract must be placed before the "return type" of the method.
- Abstract members cannot have any body definition.
- Abstract classes cannot be used for dependency injection
What are the things that are allowed in an Abstract Class?
- An abstract class may or may not have any abstract members at all. It is not mandatory to have abstract members in an abstract class.
- An abstract class can implement interfaces, even explicit interface implementations are also allowed in the abstract classes.
- You are free to define virtual members in the abstract classes.
- Inheritance is allowed in abstract classes, an abstract class can inherit from any class or interface.
- An abstract class can be inherited from another abstract class as well. In this case, you are not compelled to implement the abstract members of the base class.
- An abstract class may or may not implement the inherited interface member or abstract member by marking them as abstract implementation.
- An abstract class can override the base class virtual method as abstract, which is known as re-abstraction.
- Shadowing is allowed for the base class concrete members by using the new keyword.
- It is also allowed to define partial abstract classes in .Net. In this case, the partial keyword must be placed after the abstract keyword.
- You are allowed to define constructors and destructors in an abstract class.
- You are allowed to define static constructors as well in the abstract classes.
- You can define constants, properties, and fields in an abstract class.
- It is allowed that the derived classes may override the abstract member as sealed or abstract (in abstract class).
- An abstract class can contain sealed methods.
What are the things that are not allowed in an Abstract Class?
- An abstract class cannot be instantiated. However, you can create reference variables of abstract classes.
- The multiple-class inheritance is not supported by abstract classes.
- An abstract class cannot be either inherited or implemented by structures.
- An abstract class cannot inherit from structures.
- The abstract methods cannot be defined as virtual because all the abstract members are virtual by default.
- You cannot define an abstract class as static.
- Access modifiers of the abstract members cannot be changed in the abstract class and the derived class. It should always be the same.
- An abstract member cannot be declared as private.
- An abstract class cannot be defined as sealed because both have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
- In .Net shadowing or hiding is not allowed for abstract members.
- The abstract member cannot be marked as sealed, but it can be overridden as sealed in the derived class.
- You cannot use the abstract keyword along with static, virtual, or override modifiers.
What is an Interface in .Net?
In C#, an interface is a contract that defines a set of methods, properties, events, and indexers that a class or struct must implement. An interface defines a contract that can be implemented by classes and structs. An interface can be defined by using the interface keyword. You can define an interface to declare capabilities that are shared among different distinct types.
The name of an interface must be a valid C# identifier name. By convention, interface names begin with a capital "I". Interfaces are used to achieve abstraction, loose coupling, and multiple inheritance in C#. Interfaces help to define a consistent way and behavior across the application.
To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.
What is the new .Net Core Interface?
Until C#8, an interface was basically a contract, it doesn’t have any implementation at all and all interface members are public and abstract by default. A class or struct that implements an interface must provide an implementation for all declared members without a default implementation provided by the interface.
But now the definition of an interface is completely changed. You can provide the default implementations for the interface methods as well same as the abstract class's concrete methods. Apart from this, you are allowed to define different access modifiers explicitly like private, protected, internal, public, protected internal, etc.
Apart from this, you can also use different method modifiers explicitly like virtual, abstract, sealed, static, partial, extern, override, etc. The new .Net Core interface uses the "most specific override rule" or "most specific implementation rule" for solving the diamond problem multiple inheritance.
What are the new allowed features in the .Net Core Interface?
- Now an interface can have a default implementation for interface members.
- The .Net Core interface supports Multiple Inheritance by the "most specific implementation rule".
- You can specify the different access modifiers explicitly like private, protected, internal, public, protected internal, etc. for the interface members.
- You are allowed to define different method modifiers explicitly like virtual, abstract, sealed, static, partial, extern, override, etc. for the interface members.
- An interface can implement the base interface abstract methods now.
- You can hide the base interface default methods or virtual methods in the inheriting interface.
- You can override the base interface virtual methods definitions in the inheriting interface.
- An interface can provide the explicit implementation of the base interface's members as well.
- The .NET 8 adds support for the serialization of properties from interface hierarchies.
What are the features that are still not allowed in the .Net Core Interface?
- Interfaces still cannot be instantiated, unlike classes in .Net.
- An interface cannot inherit from an abstract class, structure, or concrete class.
- The default interface members are not inherited in the inheriting classes, unlike class inheritance.
- You cannot define any constructors in the interface.
- You are not allowed to define any destructors in the interface. However, an interface can implement the IDisposable Interface.
- The dependency injections are still not possible in the interfaces.
- An interface cannot have constants or fields because they represent a particular implementation of data. explicitly for the interface members.
- The virtual interface members cannot be overridden as sealed in the inheriting interface.
- The overriding definition of the interface's virtual method cannot have the override keyword explicitly in the inheriting class or interface-like classes.
- The virtual methods of the interface can only be overridden explicitly in the inheriting interface or class. But you cannot have any access modifiers in explicit implementation.
- The base interface's abstract methods must be implemented with a public access modifier explicitly otherwise, you will get a compile-time error.
What is the difference between traditional .Net Interface vs .Net Core Interface?
.Net Core Interface vs. Traditional .Net Interface | ||
---|---|---|
Sr. No. | .Net Core Interface | .Net Interface |
1. | The new interface can have the method's default implementation starting from C# 8. | In traditional interface default implementation was not allowed. |
2. | You can specify the access modifiers explicitly in the .Net Core interface. | In the traditional interface, all the interface members were public by default. |
3. | You are allowed to define different method modifiers explicitly in the .Net Core interface. | On the other hand, traditional interface members cannot specify any method modifiers. |
4. | Now the derived interface can override the base interface methods. | Method implementation was not allowed in the traditional interface. |
5. | The new interface can have sealed method implementations. | However, method overriding is not allowed in the traditional interface. |
6. | You can have partial methods in the new interface. | You are not allowed to define partial members in the old interface. |
7. | ReAbstraction is possible in the interface like abstract classes in the C# 8 interfaces. | ReAbstraction is not allowed as method implementation is not possible in the C# interfaces. |
8. | Method hiding is possible in the Interface now. | Method hiding is not possible because default implementation is not allowed. |
What are the similarities between Abstract Class vs. Interface?
- Both Interfaces and Abstract classes cannot be instantiated directly.
- Both Interface and Abstract classes can only be used as a base or parent for inheriting.
- Both Interface and Abstract classes are public by default.
- Both Interface and Abstract classes can be defined as generic.
- Both Interfaces and Abstract classes are used for abstraction. However, the Interface requires low coding compared to the abstract class for implementation.
- Both Interfaces and Abstract classes can have abstract, virtual, and concrete members.
- Both Interface and Abstract classes are allowed to specify different access modifiers like private, public, protected, and protected internals, etc.
- The virtual members of both Interfaces and Abstract classes can be overridden in the inheriting classes.
- It is mandatory to implement the abstract members of both Interfaces and Abstract classes in the inheriting classes.
- Both Interfaces and Abstract classes cannot be inherited from structures.
What is the difference between Interfaces vs. Abstract Classes?
Interface vs. Abstract Class | ||
---|---|---|
Sr. No. | Interface | Abstract Class |
1. | The interface supports multiple inheritances starting from C# 8. | The abstract classes cannot be used for multiple inheritance |
2. | Interfaces are not allowed to define any constructors. | Abstract classes are allowed to define constructors. |
3. | Interfaces cannot have any destructors or finalizers. | Abstract classes can have a destructor. |
4. | Interfaces cannot have instance fields and constants. | You are free to define, fields, properties, and constants in the abstract class. |
5. | Interfaces are used for dependency injection. | Abstract classes can not be used for loose coupling and dependency injections. |
6. | Interface cannot inherit from any abstract class or any other base class. | An abstract class can inherit and implement from any abstract class, concrete class, or interface. |
7. | Interfaces can be inherited and implemented by structures. | However abstract classes cannot be inherited and implemented by structures. |
8. | The virtual default methods of the interface are not inherited in the derived or inherited classes. | On the other hand, all the concrete and virtual methods of the abstract classes are inherited in the derived classes. |
9. | The virtual methods of the interface can be overridden explicitly without the override keyword. | On the other hand, it is mandatory to implement the abstract methods with the override keyword explicitly. |
10. | The virtual or abstract methods of interfaces cannot be overridden as sealed in the derived classes. | On the other hand, you can override any abstract member of the abstract class as sealed in the derived classes. |
11. | Last but not least the interface keyword is used for declaring interfaces. | On the other hand, an abstract keyword is used for declaring the abstract classes. |
What are the new features introduced in the interface?
- Interface Hierarchies: The .NET 8 adds support for serializing properties from interface hierarchies in C# 11.
- Abstract Static Members: The .Net 6 introduces the feature of static abstract interface members in C# 10.
Wrap Up
Well, we know that there are a lot of new features in the interface but still there are subtle differences between them. The interface still cannot have instance fields, constants, constructors, etc. There are some restrictions on the way of implementations of abstract members of the interface as well.
There are a lot of similarities between them, but the interface has not replaced the abstract classes. Interfaces are still used for DI and writing loosely coupled codes.
Recommended
- SQL Merge Statement - EVIL or DEVIL
- Difference Between React vs. Angular
- Difference Between Constructor vs. NgOnInit in Angular
- Constructor Execution Order in C#
- Difference Between padding vs. margin in CSS
- Constructor Execution Order in C#
- Difference Between padding vs. margin in CSS
- Difference Between document.ready() vs. window.onload() in jQuery
- Difference Between target="_blank" vs target="blank" in HTML
Tags:
dotnet-interview