c# interface 和 abstract class的区别

Difference Between C# Interface vs Abstract Class

C# is an object-oriented programming language that is used in .Net framework to develop the desktop applications and web applications as well. It was developed and designed by Microsoft for common language infrastructure. It was first released in the year 2000.

C# language has been developed with a concept to make the programming simple, modern, general-purpose programming language. It also supports the software engineering principles like strong typing, dynamic checking and garbage collection. C# language has the strong features like portability, typing, Metaprogramming, Methods and Functions, Property, memory access, polymorphism, LINQ and functional programming.

C# has the common type system that divides into two parts: Reference type and value type. In C#, boxing is used for converting a value type object to a value of corresponding reference type. Boxing is Implicit in C#. Unboxing is referred as converting a value of reference type object into the value of value type. C# requires an explicit type cast for unboxing.

C# provides full support for object-oriented concepts that are Encapsulation, Abstraction, Inheritance, and Polymorphism. In C#, classes are defined that defines the structure of program and fields. A class is mainly made up of three things that are a name, attributes, and operations. Objects are referred to as instances of classes, which helps in accessing the methods and fields of the class.

In C#, Encapsulation refers to bind the member function and data member into a single class. The class encapsulates the set of methods, properties, and attributes of its functionalities to other class.  In C#, polymorphism can be achieved using method overloading and method overriding. It is also referred to as static polymorphism (Compile time) and dynamic polymorphism (Runtime). Inheritance is also used to inherit the base class members by a derived class.

In C#, Abstraction is used to hide the unnecessary details. It means it will focus on what object can do, rather than how it works. It is used for large and complex programs. Abstract class vs interface C# are used to achieve this. In an abstract class, we can create the functionality and that needs to be implemented by the derived class. The interface allows us to define the functionality or functions but cannot implement that. The derived class extend the interface and implement those functions.

Head To Head Comparison Between C# Interface vs Abstract Class (Infographics)

Below is the top 6 difference between C# Interface vs Abstract Classc# interface 和 abstract class的区别_第1张图片

Key differences between C# Interface vs Abstract Class

Both C# Interface vs Abstract Class are popular choices in the market; let us discuss some of the major Difference Between C# Interface vs Abstract Class:

  • In C#, A class inherits one or more interfaces. But a class can inherit only one abstract class.
  • In C#, An interface cannot have the constructor declaration. An abstract class can have the constructor declaration.
  • In C#, an interface is used to define the outer abilities of a class. An abstract class is used to define the actual identity of a class and it is used as the object or the same type.
  • In C#, an interface is used if various implementations only shared method signatures. An abstract class is used if various implementations are of the same kind and use the same behavior or status.
  • In C#, if a new method has been added to the interface then we need to track where all the interface was implemented and add the implementation of that method as well. In an abstract class, if a new method has been added then we have an option to add the default implementation and therefore all the existing code work properly.

C# Interface vs Abstract Class Comparison Table

The primary Comparison between C# Interface vs Abstract Class are discussed below:

The basis of comparison Between C# Interface vs Abstract Class

C# Interface

C# Abstract Class

Access Specifier In C#, Interface cannot have access specifier for functions. It is public by default. In C#, an abstract class can have access specifier for functions.
Implementation In C#, an interface can only have a signature, not the implementation. An abstract class can provide complete implementation.
Speed Interface is comparatively slow. An abstract class is fast.
Instantiate Interface is absolutely abstract and cannot be instantiated. An abstract class cannot be instantiated.
Fields Interface cannot have fields. An abstract class can have defined fields and constants.
Methods Interface has only abstract methods. An abstract class can have non-abstract methods.

Conclusion – C# Interface vs Abstract Class

In C#, an Abstract class vs interface C# have been used for data abstraction. An interface is better than abstract class when multiple classes need to implement the interface. The member of the interface cannot be static. The only complete member of an abstract class can be static.

C# does not support multiple inheritances, interfaces are mainly used to implement the multiple inheritances. As a class can implement more than one interface and only inherit from one abstract class. An interface is mainly used only when we do not require the implementation of methods or functionalities. An abstract class is used when we do require at least default implementation.

These both C# Interface vs Abstract Class are great object-oriented programming concepts that are used highly in developing applications as per the requirement. It is purely selected the by the technical leads with which they are more comfortable and the business requirement. These both C# Interface vs Abstract Class are easy to use and simple to learn in any programming language.

Recommended Article

This has a been a guide to the top differences between C# Interface vs Abstract Class. Here we also discuss the C# Interface vs Abstract Class key differences with infographics, and comparison table. You may also have a look at the following articles to learn more –

 

From https://www.educba.com/c-sharp-interface-vs-abstract-class/

你可能感兴趣的:(C#)