What is different between an abstract and an Inter

2019-06-26 16:37发布

What is different between an abstract and an Interface class in C#?

8条回答
成全新的幸福
2楼-- · 2019-06-26 17:14
仙女界的扛把子
3楼-- · 2019-06-26 17:22

I would explain this through the usage. Abstract class can be used when there is only one hierarchy, additionally without default implementation; while interface can be used across hierarchies (horizontally), often referred to as a behavior.

Interface is also an abstraction and in c# substitutes multiple class inheritance, so this may be confusing, but you have to distinguish when to use what.

Hope this helps, Robert

查看更多
别忘想泡老子
4楼-- · 2019-06-26 17:26

the level of interface is higher than abstract.
when u're design the strcuture, draw the uml, u should use interface.
when u're implement, then u should use abstract to extract repeat things.

anyway, the different is not only a syntax problem..
hope it helps.

查看更多
ら.Afraid
5楼-- · 2019-06-26 17:29

The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes. When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

查看更多
SAY GOODBYE
6楼-- · 2019-06-26 17:30

An interface is not a class, it is just a contract that defines the public members that a class must implement.

An abstract class is just a class from which you cannot create an instance. Normally you would use it to define a base class that defines some virtual methods for derived classes to implement.

查看更多
Viruses.
7楼-- · 2019-06-26 17:30

A class can implement multiple interfaces but can only inherit from one abstract class.

An abstract class can provide implementation for it's methods. An interface cannot provide implementations.

查看更多
登录 后发表回答