C + + interfaces and abstract classes

Source: Internet
Author: User

In fact, the difference between the abstract class and interface has been unclear, and recently learning "design mode", during the use of a lot of C + + polymorphic knowledge. This is just suddenly discovered, is should be sorted out this knowledge. But in the reading of books, Internet access to information, found an article summed up well. Therefore, reproduced (Bo master do not blame) and slightly modified as follows:

Abstract Classes: Abstract classes are special classes, but cannot be instantiated (classes that define one or more pure virtual functions are called abstract classes ), and other characteristics of classes, and it is important that abstract classes can include abstract methods, which are not available to ordinary classes. But it can also include common methods. 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 that can override the base class's abstract methods or not, and if not overwritten, their derived classes must overwrite them. Although you cannot define an instance of an abstract class, you can define its pointer, and a pointer to an abstract class is actually assigned to an instantiated object of its inheriting class, so that the implementation of different subclasses can be well encapsulated by using the pointer uniformly, which is greatly applied in the process of pattern design.

Interface: An interface is a concept. It is implemented in C + + with abstract classes and interface in C # and Java.

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

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

In addition, the interface has the following features:
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 difference between an abstract class and an interface:
1. The concepts of interfaces and abstract classes are different. An interface is an abstraction of an action, and an abstract class is an abstraction of the root. The abstract class represents what this object is. The interface represents what this object can do. For example, men, women, these two classes (if it's a class ...) ), their abstract class is human. They are all people can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to implement it. Therefore, in high-level languages, a class can inherit only one class (abstract class) (just as a human cannot be both biological and non-living), but it can implement Multiple interfaces (eating interface, walking interface).

2. Abstract class when defining a type method, it can give the implementation part of the method, or not, and for the interface, the method defined in it cannot give the implementation part.
3. Inheriting classes differ in the implementation of the methods involved. The abstract method that the inheriting class defines for the abstract class can be used without rewriting, that is to say, the method of the abstract class can be used, and for the method or property defined by the interface class, the corresponding method and property must be implemented in the inheriting class.
4. Interfaces can be used to support callbacks, and inheritance does not have this feature.
5. Abstract classes cannot be sealed, one class can implement several interfaces at a time, but only one (abstract class) parent is extensible.
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. (interfaces) similar to non-abstract classes, an abstract class must also provide its own implementation for all members of the interface 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. Abstract classes implement a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable seating subclass is implemented.
9. A good interface definition should be functional, not multi-functional, or cause interface pollution.   (If a class is simply to implement one of the functions of this interface, but 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 Address: http://blog.csdn.net/bmzyDream_007/article/details/4157560

Interface

Abstract class

Multiple inheritance

Support

Not supported

Type restrictions

No

Yes, only reference type

Method implementation

The method implementation must be given in the inheritance type

The inheritance class can not give a

Scalability

More Trouble

Relatively flexible

Multilayer inheritance

More trouble, need to use virtual function

More flexible

C + + interfaces and abstract classes

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.