How to choose Class in. NET application design, Abstract class and Interface

Source: Internet
Author: User

Key words:

type– type

Class-Classes

Abstract-abstracted

Interface-Interface

Member-Members

Method-Methods

Property-Properties

Pre-Knowledge: You should understand when reading this article. NET programming knowledge and has mastered class, Abstract class and interface all knowledge. Here I'll just briefly introduce their basic knowledge. Examples of this article are written in C #. Expect you to have a certain understanding of C # programming.

Body:

We cannot create an abstract class or interface instance (INSTANCE). Let's look at their differences from the definition of abstract class and interface. The abstract class can contain abstract Methods and abstract Properties, as well as other members, like normal class. Interface can only contain abstract methods and properties (attributes). All methods and properties in interface do not need to add the abstract and public keywords, because these two keywords are default in interface. Examples are as follows:

1 //abstarct Class2  Public Abstract classVehicles3       {4         Private intNoofwheel;5         Private stringcolor;6          Public Abstract stringEngine7         {   8             Get;9             Set;Ten         } One          Public Abstract voidAccelerator (); A       } -  -       //Interface the  Public InterfaceVehicles -       { -         stringEngine -         {    +             Get; -             Set; +         } A         voidAccelerator (); at}

In general, it is preferable to use class or abstract class rather than interface at design time. The main disadvantage of interface is that the flexibility is rather poor. Once you have defined the interface, the members of it are fixed. If you are adding a new method to a program that has already been published, you will break the type (class,struct, etc.) that you have implemented for that interface. Because you have to implement new methods in the type you already have, your program will not compile.

For example, class car and train implement interface vehicles. Now we're going to add a method brake () to the interface vehicles. If we compile the class car and train now, the compiler will make an error.

1  Public InterfaceVehicles2       {3 ... ..4         //newly added methods5         voidBrake ();6       }7 to fix this error, we have to implement method brake () in class car and train. The sample code is as follows:8  Public classCar:vehicles9 {Ten ... .. One          Public voidBrake () A         { -System.Console.WriteLine ("Stop your Car "); -         } the }  -  -  Public classTrain:vehicles - { + ... .. -          Public voidBrake () +         { ASystem.Console.WriteLine ("Stop your Train "); at         } -}

If we use abstract classes or normal class vehicles, we just need to add the brake () method to the class vehicles and implement this method. We then decide whether to overwrite the brake () method in the class car or train, depending on the specific needs.

 Public Abstract class Vehicles       {        ...         // The newly added method does not need to overwrite this method in its subclasses. public        void  Brake ()        {            System.Console.WriteLine ("  Stop your Vehicles ");         }      }

Class gives you more flexibility. You can add any members to class, as long as you add not the abstract method (that is, you want to provide a concrete implementation of the method). This does not affect classes that inherit from the class, and there is no need to change any of the existing code.

Design principles

• Priority is given to using class or abstract class instead of interface.

• Use abstract class instead of interface to reduce the coupling between class inheritance levels.

• Using interface, if you need to give a value type implementation (value type, like a struct is a value type) polymorphic inheritance (polymorphic Hierarchy). (a value type cannot inherit from another type except inheriting from interface).

• Consider using interface in cases where multiple inheritance is required.

How to choose Class in. NET application design, Abstract class and 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.