Home Update When to make use of an summary class vs. interface in C#

When to make use of an summary class vs. interface in C#

13
When to use an abstract class vs. interface in C#


When designing purposes, you will need to know when to make use of an summary class and when to make use of an interface. Although summary lessons and interfaces appear comparable in some methods, there are key variations that may decide which is the only option for what you’re making an attempt to perform. In this weblog put up I’ll talk about these variations and find out how to determine when to make use of which.

The brief reply: An summary class means that you can create performance that subclasses can implement or override. An interface solely means that you can outline performance, not implement it. And whereas a category can prolong just one summary class, it could actually benefit from a number of interfaces. 

C# summary class defined

An summary class is a particular kind of sophistication that can’t be instantiated. An summary class is designed to be inherited by subclasses that both implement or override its strategies. In different phrases, summary lessons are both partially carried out or not carried out in any respect. You can have performance in your summary class—the strategies in an summary class will be each summary and concrete. An summary class can have constructors—that is one main distinction between an summary class and an interface. You can benefit from summary lessons to design parts and specify some degree of frequent performance that have to be carried out by derived lessons.

C# interface defined

An interface is principally a contract—it doesn’t have any implementation. An interface can comprise solely methodology declarations; it can’t comprise methodology definitions. Nor can you will have any member knowledge in an interface. Whereas an summary class could comprise methodology definitions, fields, and constructors, an interface could solely have declarations of occasions, strategies, and properties. Methods declared in an interface have to be carried out by the lessons that implement the interface. Note {that a} class can implement multiple interface however prolong just one class. The class that implements the interface ought to implement all its members. Like an summary class, an interface can’t be instantiated.

Key similarities between summary lessons and interfaces in C#

There are 4 key similarities between interfaces and summary lessons.

Behavior with out implementation: The C# programming language supplies each interfaces and summary lessons to outline conduct with out implementation. You can benefit from an interface or an summary class to outline a contract that have to be adopted by…



Source hyperlink

LEAVE A REPLY

Please enter your comment!
Please enter your name here