Abstract class and interface suggestions

Source: Internet
Author: User

Abstract types and interfaces are two mechanisms used to specify programming abstraction. Abstract an agreement that must be followed by a designated successor or implementer. Abstract types can provide implementation details, while interfaces cannot provide any implementation details.

  • Classes may only inherit from a base class, so if you want to use abstract classes to provide polymorphism for a group of classes, these classes must all inherit from that class.
  • Abstract classes may also provide implemented members. Therefore, you can use abstract classes to ensure that a specified number of functions are the same, but you cannot use interfaces to do so.

Here are some suggestions to help you decide whether to use interfaces or abstract classes to provide polymorphism for components.

  • If you want to create multiple versions of a component, create an abstract class. Abstract classes provide a simple way to control component versions. By updating the base class, all inheritance classes are automatically updated with the change. On the other hand, the interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface.
  • If the created function is used across a wide range of different objects, the interface is used. Abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes.
  • If you want to design small and concise functional blocks, use interfaces. If you want to design a large functional unit, use an abstract class.
  • If you want to provide common implemented functions among all the implementations of a component, use an abstract class. Abstract classes allow partial implementation classes, while interfaces do not include the implementation of any member.

Reference: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/vbcon/html/vbconw.actclassesversusinterfaces. asp

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.