Tech Point Fundamentals

Sunday, September 13, 2020

Abstract Class vs Interface C# 8

Do interfaces replace the abstract classes in C# 8?

AbstractClass-Vs-Interface

  As we know Microsoft has introduced a lot of new interesting features for interfaces in C# 8. The interface definition is completely changed in C# 8. Although abstract classes and interfaces now seem similar in more than one way, still there are subtle differences between the two. In this article, we will see them in detail.


Watch our videos here




Introduction

An Abstract Class without any implementation just looks like an Interface. Probably "Difference Between Abstract Class and Interface" is the most frequent question being asked in the .Net world. However, the new features introduced in C# 8 completely changed the answer to this question.





What is the Abstract Keyword in C#?

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces


The abstract modifier indicates incomplete implementation or partial implementation. The keyword abstract is used before the class or method to declare the class or method as abstract. Also, the abstract modifier can be used with indexers, events, and properties.




What is the Abstract Method in C#?

A method declared with an abstract keyword is called an abstract method, and an abstract method has nobody. An abstract method is by default a virtual method. If you are declaring any method as abstract then the class must be abstract otherwise you will get a compile-time error.


You cannot use static and virtual modifiers in an abstract method declaration. Its implementation must be provided by derived classes. 


Until C# 8 you can declare abstract methods only inside the abstract class but, in C# 8 you can have abstract methods in the interface as well. 


Please read more about the abstract method here.




What is Abstract Class in C#?

An abstract class is a special class that cannot be instantiated. An abstract class can be declared with an abstract keyword. An abstract class may or may not have any abstract members. An abstract class can also have non-abstract methods (concrete methods). 


You can declare an abstract class without defining any abstract method in it, but if any method is abstract, then you must have to make the class abstract.


We can use an abstract class only as a base class and all the derived classes must have to implement all the abstract members. An abstract method must be implemented in all non-abstract classes using the override keyword.


Please read more about the abstract class here.




Abstract Class Key Points

    1. An abstract class may have no abstract member at all but if any member is declared as abstract, then the class must be abstract.
    2. An abstract class cannot be instantiated.
    3. An abstract class cannot be static.
    4. An abstract class can contain constructors or destructors.
    5. An abstract class can have constants and fields.
    6. You cannot use an abstract modifier along with static, virtual, and override modifiers.
    7. An abstract class cannot support multiple-class inheritance.
    8. An abstract class cannot be inherited by structures.
    9. An abstract class cannot be inherited from structures.
    10. An abstract class can inherit from a single base class and one or more interfaces.
    11. The inheriting class must override all the abstract members with the override keyword explicitly. 
    12. The inheriting class may override the abstract member as sealed or abstract (in the abstract class).

What is Interface in C#?

It is an entity that is defined by the keyword interface. The implementation of the interface members will be given by the inheriting class either implicitly or explicitly. Interfaces specify what a class must do and not how. 


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.


Please read more about the C# 8 interface here.




What's New in C# 8 Interface?

    1. An Interface can have a default implementation in C# 8. 
    2. An Interface can have different access modifiers like private, public, protected, internal, and protected internal explicitly in C# 8.
    3. An Interface can have static interface members in C# 8.
    4. An Interface can have virtual interface members in C# 8.
    5. An Interface can have abstract interface members in C# 8.
    6. An Interface can have partial interface members in C# 8.
    7. An Interface can have sealed interface members in C# 8.
    8. An Interface can have private interface members in C# 8.
    9. An Interface can have extern interface members in C# 8.
    10. An interface can support multiple interface inheritance with diamond problem resolution in C# 8.
    11. The virtual interface members can be overridden in the inheriting class or interface explicitly in C# 8.
    12. The virtual interface member can be overridden as abstract in the inheriting interface in C# 8.
    13. The virtual interface member cannot be overridden as sealed in the inheriting interface in C# 8.
    14. You cannot override any virtual member with the override keyword in the inheriting class or interface in C# 8.
    15. You can hide the base interface concrete or virtual members in the inheriting interface in C# 8.
    16. The default interface members are not inherited in the inheriting class in C# 8.




Interface Key Points

    1. An interface cannot be instantiated.
    2. An interface cannot contain constructors or destructors.
    3. An interface cannot have constants and fields because they represent a particular implementation of data.
    4. An interface can have a default implementation.
    5. An interface can support multiple interface inheritance.
    6. An interface can be inherited by structures i.e. a structure can inherit from an interface.
    7. An interface virtual member overridden definition cannot have an override keyword explicitly in the inheriting class or interface.
    8. An interface virtual member can only be overridden explicitly in the inheriting interface or class. But you cannot have any access modifiers in explicit implementation.
    9. When you implement an interface explicitly, you can access interface members only through the instance of an interface type.
    10. Interface abstract members must be implemented with the public modifier; otherwise, the compiler will give compile-time errors.
    11. On the implementation of an interface, you must implement all of its abstract members in the inheriting class.
    12. You can override any virtual interface member as abstract in the inheriting interface.
    13. You can override any abstract interface member as abstract in the inheriting abstract class.
    14. You cannot override any virtual interface member as sealed in the inheriting class or interface.



Example





Similarities between Abstract Class and C# 8 Interface

    1. Both interface and abstract classes can have both abstract and concrete members.
    2. Both interface and abstract class can have different access modifiers like private, public, protected, and protected internals.
    3. Both interface and abstract class cannot be instantiated directly.
    4. Both interface and abstract classes cannot be inherited from structures.
    5. Both interface and abstract class virtual members can be overridden in the inheriting class.
    6. Both interface and abstract class abstract members must be implemented in the inheriting class.
    7. Both interface and abstract class can be used only as a base for inheriting.



Difference between Abstract Class and C# 8 Interface

    1. For the real abstract class, it is necessary to have at least one abstract member while an interface may not.
    2. An abstract class cannot have multiple class inheritance, but an interface can have multiple interface inheritance.
    3. An abstract class can have a constructor or destructor while an interface cannot.
    4. An abstract class can contain instance fields and constants, but the interface cannot.
    5. An abstract keyword is used to declare an abstract class while an interface keyword is used to define an interface.
    6. An abstract class cannot be inherited by structures while an interface can be inherited by structures.
    7. An abstract class can inherit from both class and interfaces while an interface can only be inherited from an interface.
    8. The virtual interface members (concrete methods) are not inherited in the derived class but all the concrete members of an abstract class are inherited.
    9. The inheriting class must have to override all the abstract members of the base abstract class with an explicit override keyword, while interface virtual members can only be overridden explicitly without the override keyword.
    10. You can override any abstract member of the abstract class as sealed in the inheriting class while you cannot in the case of the interface in C# 8.

Recommended Articles

What is the default interface implementation in C# 8

What is the interface virtual method in C# 8

What are the interface access modifiers in C# 8

Multiple Interface Inheritance and Diamond Problem in C# 8

What's New in C# 8




Live Demo


Conclusion

Although with the introduction of new features for the interface in C# 8, abstract classes and interfaces now seem similar in more ways than one. But this is not true, there are subtle differences between the two. C# 8 interface has not replaced the abstract class. There are a lot of similarities between an abstract class and an interface but still, there are a lot of differences between the two.




Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Youtube, Quora, and Pinterest for regular updates.

Video Recommendation

Watch More Videos...

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.