Similarities and differences of abstract classes and interfaces in C #

Source: Internet
Author: User
Tags define abstract

Abstract class

You can declare a class as an abstract class by placing the keyword abstract before the class definition. Abstract classes cannot be instantiated. The purpose of an abstract class is to provide a generic base class definition that can be shared by multiple derived classes. For example, a class library can define an abstract class to use as a parameter to multiple class library functions, and require programmers who use the library to provide their own class implementations by creating derived classes.

Abstract classes can contain non-abstract methods, properties, and abstract methods, properties. Both abstract classes and abstract methods must be overridden in a derived class with override.

Abstract Methods : Abstract classes can also define abstract methods. The method is to add the keyword abstract to the front of the method's return type. The abstract method is not implemented, so the method definition is followed by a semicolon instead of a regular method block. An abstract method can only be declared in an abstract class and does not contain any implementations. Derived classes of abstract classes must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method using an abstract method.

Abstract Attribute : An abstract property declaration does not provide an implementation of a property accessor, it declares only that class supports attributes, and leaves the accessor implementation to the derived class. Reference: https://msdn.microsoft.com/zh-cn/library/yd3z1377 (v=vs.110). aspx

Interface

Reference: Https://msdn.microsoft.com/zh-cn/library/ms173156.aspx

An interface is a reference type that enables the definition of a set of related functions. An interface can contain a method, property, event, indexer, or any combination of these four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. Interface members will automatically be public, so they will not contain any access modifiers. Members also cannot be static. To implement an interface member, the corresponding member of the implemented class must be public, non-static, and have the same name and signature as the interface member.

The interface has the following properties:
-Interface and abstract base class. Any class or struct that implements an interface must implement a link to all its members.
-the interface cannot be instantiated directly. Its members are implemented by any class or struct that implements the interface.
-An interface can contain events, indexers, methods, and properties.
-the interface does not contain the implementation of the method.
-classes or structs can implement multiple interfaces. A class can inherit a base class and implement one or more interfaces.

Same point

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);

Different points

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.

Reference: http://kb.cnblogs.com/page/41836/


Similarities and differences of 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.