The difference between abstract classes and interfaces in C #

Source: Internet
Author: User

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. cannot be instantiated;
2. Contains a method declaration that is not implemented;
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 class is more defined in a series of closely related classes, and the interface is mostly loose, but the implementation of a function of the class;
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 may implement several interfaces at a time, but only one parent class can be extended;
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 of the abstract class implementation is virtual by default, but the interface method in the class that implements the interface defaults to non-virtual, 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 variable seating subclasses are implemented;
9. A good interface definition should be functional, not multi-functional, or cause interface pollution. If a class just realizes the function 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, then use the interface;
3. If you are designing large functional units, use an abstract class. Use abstract classes 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 figurative metaphors I've seen on the internet.
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 specific iron gate or wooden door (polymorphic); and can only be the door, you can not 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)).

The difference between abstract classes and interfaces 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.