The difference between abstract classes and interfaces in C #

Source: Internet
Author: User
Tags abstract inheritance

One, abstract class:

Abstract classes are special classes, but cannot be instantiated; In addition, there are other attributes of the class; It is important that abstract classes can include abstract methods, which are not possible for 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, an abstract class can be derived from an abstract class that can override the base class's abstract methods or not, and its derived classes must overwrite them if they are not overwritten.

Second, the interface:

Interfaces are reference types, similar to classes, and have three points in common with abstract classes:

1, can not be instantiated;

2, including the implementation of the Declaration of methods;

3. Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members);

In addition, the interface has the following characteristics:

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

Iii. the difference between abstract classes and interfaces:

1. Classes are abstractions of objects, abstract classes can be understood to treat classes as objects, and abstract classes are called abstract classes. While interfaces are just a specification or regulation of behavior, Microsoft's custom interface always comes with a able field, proving that it is a class "I can do ..." ". Abstract classes are more defined in a series of tightly related classes, while interfaces are mostly loose but implement a functional class.

2. The interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method;

3. A class can implement several interfaces at a time, but can only extend one parent class

4. Interfaces can be used to support callbacks, and inheritance does not have this feature.

5. Abstract classes cannot be sealed.

6. The concrete method of the abstract class implementation defaults to virtual, but the interface method in the class that implements the interface is not virtual by default, but you can also declare it as virtual.

7. (interface) Like a Non-abstract class, an 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.

8. The abstract class implements a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable seating subclasses are implemented.

9. A good interface definition should be a specific function, rather than multi-functional, otherwise cause interface pollution. If a class only implements one of the functions of the interface and has to implement other methods in the interface, it is called interface pollution.

10. Avoid the use of inheritance to achieve the building function, but the use of black box reuse, that is, the combination of objects. Because the level of inheritance increases, the most immediate consequence is that when you call a category in this class group, you must load them all into the stack! The consequences are conceivable. (combined with the theory of stack understanding). At the same time, interested friends can note that Microsoft is building a class, many times the use of the method of object composition. For example, in ASP.net, the page class has attributes such as serverrequest, but in fact they are all objects of a class. Using this object of the page class to invoke the methods and properties of another class, this is a very basic design principle.

11. If an abstract class implements an interface, you can map the methods in an interface to an abstract method in an abstract class without having to implement them, and implement the methods in an interface in a subclass of an abstract class.

Iv. use of abstract classes and interfaces:

1. If you expect to create multiple versions of a component, create an abstract class. Abstract classes provide a simple way to control the component version.

2. If you create a feature that will be used across a wide range of objects, use an interface. If you want to design a small and concise feature block, use an interface.

3. If you want to design a large functional unit, use an abstract class. If you want to provide common implemented functionality between all implementations of a component, use an abstract class.

4. Abstract classes are primarily used for closely related objects, and interfaces are suitable for providing common functionality for unrelated classes.

The following is a few images I saw on the internet, really very good, hehe:

1. Airplanes can fly, birds can fly, they all inherit the same interface "Fly"; but F22 belong to the abstract class of aircraft, pigeons belong to the abstract class of birds.

2. Just like the gate doors are doors (abstract class), you want a door I can't give (can't instantiate), but I can give you a specific iron gate or wooden door (polymorphic), and can only be a door, you can not say it is a window (single inheritance), a door can have a lock (interface) can also have a doorbell (more implementation). The door (abstract class) defines what you are, and the interface (lock) prescribes what you can do (an interface is best to do only one thing, and you can't ask for a lock to make a sound (interface pollution)).

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.