The difference between an interface and an abstract class

Source: Internet
Author: User

First introduce the concepts of interfaces and abstract classes:

    • Interface (interface): A contract used to define a program. The class or structure that implements the interface is strictly consistent with the definition of the interface. With this agreement, you can throw away the limitations of programming languages (theoretically). Interfaces can inherit from multiple base interfaces, and classes or structs can implement multiple interfaces. An interface can contain methods, properties, events, and indexers. The interface itself does not provide an implementation of the members it defines. The interface specifies only the members that the class or interface that implements the interface must provide.
    • Abstract class: A class that contains abstract methods, which do not need to be included. So what is an abstract method? Abstract methods are methods that have no method content, only a method name and a parameter list. to end with; To mark his differences, add an abstract before his return type. and add an abstract before class. In short, methods and classes that are modified by abstract are abstract methods and classes.

interface to the same point as the abstract class:

1, can not be instantiated;

2, including the non-implementation of the method declaration;

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);

The difference between an interface and an abstract class:

1. A class is an abstraction of an object, which can be understood as a class as an object, an abstract class called an abstraction. and an interface is just a specification or a rule of behavior.

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 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.

7. Like a non-abstract class, an abstract class must also provide its own implementation for all members of the interface listed in the base class list for that class. However, an abstract class is allowed to map an interface method to an abstract method.

8. Abstract classes implement a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable is left to the subclass to implement.

9. 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.

10. Try to avoid using inheritance to implement the build function, but instead use black box multiplexing, which is the object combination. Because of the increasing level of inheritance, the most immediate consequence is that when you call one of these classes, you have to load them all into the stack! The consequences are conceivable. (in conjunction with the stack principle). At the same time, the heart of friends can notice that Microsoft in the construction of a class, many times the use of the object combination method. For example, in ASP. NET, the page class has properties such as server request, 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 is a very basic design principle.

11. If an abstract class implements an interface, it is possible to map a method in an interface to an abstract class as an abstract method without having to implement the method in the subclass of the abstract class.

Reprint: http://blog.csdn.net/cjr15233661143/article/details/8592527

The difference between an interface and an abstract class

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.