The difference between abstract classes and interfaces in C #

Source: Internet
Author: User

Original: The difference between abstract classes and interfaces in C #

It is easy to confuse abstract classes and interfaces when programming, and the following is a conceptual explanation of the differences between abstract classes and interfaces:

first, abstract class:

Class with the abstract modifier is an abstract class, an abstract class is a Special class, but cannot be instantiated, you can create a variable whose type is an abstract class, and it points to an instance of a specific subclass In addition, having other characteristics of the class, it is important that abstract classes can include abstract methods, which are not available to ordinary classes. Abstract methods can only be declared in an abstract class and do not contain any implementations, and derived classes must overwrite them. In addition, abstract classes can derive from an abstract class, which can override the base class's abstract methods or not overwrite them.

second, the interface:

An interface is a reference type, similar to a class, with three points: 1, cannot be instantiated, 2, contains a method declaration that is not implemented, 3, a derived class must implement a method that is not implemented, an abstract class is an abstract method, and an interface is all members (not just methods including other members);

The interface has the following characteristics:

An interface can contain properties, indexers, events, and those members are defined as public, in addition to methods. In addition, you cannot include any other members, such as constants, fields, constructors, destructors, static members. A class can inherit directly from multiple interfaces, but only a single class (including abstract classes) is inherited directly.

The grammatical differences between the two:

1. Abstract classes can have construction methods, and interfaces cannot have constructors.

2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface

3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.

4. An abstract method in an abstract class can have an access type of public ,protected, but the abstract method in the interface is only of the public type, and the public abstract type is the default.

5. Abstract classes can contain static methods, and interfaces cannot contain static methods

6. abstract classes and interfaces can contain static member variables, the access type of static member variables in the abstract class can be arbitrary, but the variables defined in the interface can only be public static final type, and the default is public static Final type.

7. A class can implement multiple interfaces, but can inherit only one abstract class.

8. Interfaces can be used to support callbacks, and inheritance does not have this feature. 9. 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 you can declare it as virtual.

use of abstract classes and interfaces:

1. If you expect to create multiple versions of a component, create an abstract class.   Abstract classes provide an easy way to control the component version. 2. If you create a feature that will be used across a wide range of heterogeneous objects, use an interface.   If you want to design a small and concise function block, use the interface.   3. If you are designing large functional units, use an abstract class. Abstract classes are used if you want to provide common, implemented functionality across all implementations of a component. 4. Abstract classes are primarily used for closely related objects, while interfaces are suitable for providing common functionality for unrelated classes.

5. A good interface definition should be functional, not multi-functional, or cause interface pollution. If a class just implements one of the functions of this interface, and has to implement other methods in the interface, it is called interface pollution.

The difference between abstract classes and interfaces in C #

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.