开发者

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

What is different between an abstract and an Inte开发者_如何学Pythonrface class in C#?


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.


Rather than writing whole thing here..

try http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx


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.


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.


Google "abstract class vs interface" and you'll get lots of explanatory articles...


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

Also, abstract classes may have some functions defined but interfaces will not have any function definition and the deriving class must define all of them.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜