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 vs Abstract Class Infographics

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 SpecifierIn C#, Interface cannot have access specifier for functions. It is public by default.In C#, an abstract class can have access specifier for functions.
ImplementationIn C#, an interface can only have a signature, not the implementation.An abstract class can provide complete implementation.
SpeedInterface is comparatively slow.An abstract class is fast.
InstantiateInterface is absolutely abstract and cannot be instantiated.An abstract class cannot be instantiated.
FieldsInterface cannot have fields.An abstract class can have defined fields and constants.
MethodsInterface 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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值