The difference between an interface and an abstract class in C #

Source: Internet
Author: User

Everyone is easy to confuse the two, I also, in listening to the Li Jianzhong Teacher's design model, he also old to say the abstract class interface, I was more confused, so found some online information.
First, abstract class:
Abstract classes are special classes that cannot be instantiated, except that they have other characteristics of classes, and it is important that abstract classes include abstract methods, which are not available to ordinary classes. Abstract methods can only be declared in an abstract class and do not contain any implementations, and derived classes must overwrite them. In addition, abstract classes can derive from an abstract class that can override the base class's abstract methods or not, and if not overwritten, their derived classes must overwrite them.

Second, the interface:
Interfaces are reference types, similar to classes, and are similar to abstract classes with three points:
1, can not be instantiated;
2, including the non-implementation of the method declaration;
3. Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members);

In addition, the interface has the following features:
an interface can contain properties, indexers, events, and those members are defined as public, in addition to methods. In addition, you cannot include any other members, such as constants, fields, constructors, destructors, static members. A class can inherit directly from multiple interfaces, but only a single class (including abstract classes) is inherited directly.


third, the difference between abstract class and interface:
1. A class is an abstraction of an object, which can be interpreted as a class as an object, an abstract class called an abstraction. While an interface is just a specification or rule of behavior, Microsoft's custom interface is always followed by a able field, proving that it is a class "I can do ... ". Abstract classes are more defined in a series of tightly related classes, whereas interfaces are mostly in classes where relationships are loose but all implement a function.
2. The interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method;
3. A class can implement several interfaces at a time, but can only extend one parent class
4. Interfaces can be used to support callbacks, and inheritance does not have this feature.
5. Abstract classes cannot be sealed.
6. The concrete method implemented by the abstract class defaults to virtual, but the interface method in the class that implements the interface defaults to non-virtual, and of course you can declare it as virtual.
7. (interfaces) similar to non-abstract classes, an abstract class must also provide its own implementation for all members of the interface listed in the base class list of the class.   However, an abstract class is allowed to map an interface method to an abstract method.
8. Abstract classes implement a principle in oop, separating mutable from immutable.   Abstract classes and interfaces are defined as immutable, and the variable seating subclass is implemented.
9. A good interface definition should be functional, not multi-functional, or cause interface pollution.   If a class just implements one of the functions of this interface, and has to implement other methods in the interface, it is called interface pollution.
10. Try to avoid using inheritance to implement the build function, but instead use black box multiplexing, which is the object combination. Because of the increasing level of inheritance, the most immediate consequence is that when you call one of these classes, you have to load them all into the stack! The consequences are conceivable. (in conjunction with the stack principle). At the same time, the heart of friends can notice that Microsoft in the construction of a class, many times the use of the object combination method. For example, in ASP. NET, the page class has properties such as server request, but in fact they are all objects of a class.   Using this object of the page class to invoke the methods and properties of another class is a very basic design principle.
11. If an abstract class implements an interface, it is possible to map a method in an interface to an abstract class as an abstract method without having to implement the method in the subclass of the abstract class.
   
Iv. use of abstract classes and interfaces:
1. If you expect to create multiple versions of a component, create an abstract class. Abstract classes provide an easy way to control the component version.
2. If you create a feature that will be used across a wide range of heterogeneous objects, use an interface. If you want to design a small and concise function block, use the interface.
3. If you are designing large functional units, use an abstract class. Abstract classes are used if you want to provide common, implemented functionality across all implementations of a component.
4. Abstract classes are primarily used for closely related objects, while interfaces are suitable for providing common functionality for unrelated classes.


Here are some of the images I saw on the internet, really very good, hehe:
1. Aircraft will fly, birds will fly, they all inherit the same interface "Fly"; but F22 belongs to the abstract class of airplanes, the pigeons belong to the abstract class of birds.
2. Like iron doors are doors (abstract class), you want a door I can't give (can't instantiate), but I can give you a concrete iron gate or wooden door (polymorphic); and only the door, you can't say it is a window (single inheritance); A door can have a lock (interface) can also have a doorbell (multi-implementation). The door (abstract class) defines what you are, and the interface (lock) specifies what you can do (an interface is best to do only one thing, and you cannot ask for a lock to make a sound (interface pollution)).

Original address: http://www.cnblogs.com/lovemyth/archive/2008/09/08/828909.html

The difference between an interface and an abstract class in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.