C # Basic Learning--abstract classes and interfaces

Source: Internet
Author: User

1. Abstract class

Abstract class is a special class, it has the following characteristics:

    • Abstract methods are used only for declarations, not implementations, and can be seen as virtual methods that do not implement the body.
    • Abstract classes cannot be instantiated. In addition, there are other characteristics of the class.
    • Abstract classes can but do not have to have abstract attributes and abstract methods, but once you have an abstract method, you must declare the class as an abstract class.
    • The specific derived class must override the abstract method of the base class.
    • Abstract classes can derive from another abstract class, which can override the base class's abstract methods or not, and if not overwritten, their derived classes must overwrite them.

2. Interface (interface)

An interface is a reference type, similar to an abstract class but different from an abstract class, which has the following characteristics:

    • cannot be instantiated.
    • Only the method declaration of the implementation can be included.
    • Members can include methods, properties, indexers, and events.
    • An interface cannot contain constants, fields (domains), constructors, destructors, or static class members.
    • All members of the interface are public by default, so there cannot be a private modifier in the interface.
    • Derived classes must implement all members of an interface.
    • A class can implement multiple interfaces directly, separating the interfaces with commas.
    • An interface can have multiple parent interfaces, and the class that implements the interface must implement all the members of all the parent interfaces.

3. The same point of the abstract class and interface

    • Can be inherited.
    • cannot be instantiated.
    • Can contain method declarations.
    • Derived classes must implement methods that are not implemented

4. The difference between abstract classes and interfaces

    • Abstract class is an incomplete class that needs further refinement, and the interface is just a specification or rule of behavior, and Microsoft's custom interface is always followed by the able field, proving that it is the expression class "I can do ...".
    • Abstract base classes can define fields, properties, and method implementations. An interface can only define properties, indexers, events, and method declarations, and cannot contain fields.
    • Abstract classes are more defined between classes that are closely related to columns, and most of the interfaces are defined in classes in which the relationship is loose but a function is implemented.
    • Interface basically does not have any specific characteristics of inheritance, he only promised to be able to invoke the method.
    • Interfaces can be implemented in multiple implementations, and abstract classes can only be inherited by a single.
    • Interfaces can be used to support callbacks, and inheritance does not have this feature.
    • Abstract classes cannot be sealed.
    • The concrete method implemented by the abstract class defaults to virtual, but the interface method in the class that implements the interface defaults to non-virtual, and of course it can be declared as virtual.
    • Interfaces are similar to non-abstract classes, the abstract class must also provide its own implementation for all members of the interfaces listed in the base class list of the class. However, an abstract class is allowed to map an interface method to an abstract method.
    • If an abstract class implements an interface, it is possible to map the methods in an interface to an abstract class as an abstract method without having to implement it, but instead implement the methods in the interface in the subclasses of the abstract class.

5 use of abstract classes and interfaces

C # Basic Learning--abstract classes and interfaces

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.