Select between class and Interface

Source: Internet
Author: User

The signature of a group of Members required by the interface definition implementer. The interface cannot provide implementation details of members. For example, the icollection interface defines the members related to the use of the set. Each class implementing this interface must provide the Implementation Details of these members. Class can implement multiple interfaces.

Class defines the member signatures and implementation details for each member.Abstract(In Visual BasicMustinheritThe behavior of a class is the same as that of an interface or a common class, that is, a member can be defined and implementation details can be provided, but this is not required. If the abstract class does not provide implementation details, the specific class inherited from the abstract class needs to provide implementation.

Although abstract classes and interfaces support separation of protocols and implementations, the interface cannot specify new members in later versions. abstract classes can add members as needed to support more functions.

Define classes rather than interfaces.

In later versions of the library, you can safely add new members to the class. For interfaces, you can only modify existingCodeTo add members.

Use an abstract (mustinherit in Visual Basic) class instead of an interface to separate the protocol and implementation. If you need to provide the value type of the multi-State hierarchy, you should define the interface.

The value type must be inherited from valuetype and can only beValuetypeTherefore, the value type cannot use classes to separate agreements and implementations. In this case, if the value type requires multi-state behavior, you must use the interface.

Consider defining interfaces to achieve effects similar to multi-inheritance.

If one type must implement multiple protocols, or the protocols apply to multiple types, use interfaces. For example, idisposable is implemented by many types used in different situations. If the class inherited from the base class is required to be disposed of, the class hierarchy is not flexible. Memorystream and others should inherit stream-based protocol classes from their parent classes, which cannot be disposed.

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.