Base class vs Interface

Source: Internet
Author: User

This article refers to section 13.11 of the CLR via C #.

Whether the base class or interface should be designed, this problem cannot be generalize, and some guiding principles are provided below:

1. Is_a relationship (meaning belonging to, for example, cars belonging to vehicles) vs can_do relationships (meaning that a type can convert its own instance to another type) can inherit only one implementation. If a derived type does not have a is_a relationship to the base class, the base class should not be used and the interface should be used. An interface implies a CAN_DO relationship. If more than one object type has CAN_DO functionality, the interface is used. For example, a type can convert its own instance to another type, a type can serialize its own instance, and so on. Note that value types must derive from System.ValueType, all of which they cannot derive from any base class. In this case, you must use a CAN_DO relationship and define an interface;

2. Easy to use for developers, defining a new type derived from a base class is often much easier than implementing all the methods of an interface. A base type can provide a number of features, and all derived types may only need to be slightly altered for their behavior. If an interface is provided, the new type must implement all members;

3. Consistency implementation no matter how good the contract document of an interface is, there is no guarantee that anyone can achieve it correctly. In fact, COM is quite vulnerable to this problem, which causes some COM objects to be used only correctly in Microsoft Office Word. If you provide a good default implementation for a base type, then you start with a type that works and is well tested, and you only need to modify it to suit your needs;

4. Version control adds a method to the base class, and the derived class inherits the new method. The first thing to use is a working type, and the user's source code doesn't even need to be recompiled. Adding a new member to an interface forces the successor of the interface to change its source code and recompile.

The interface can also be provided, and the base class of the interface is implemented.

Base class vs Interface

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.