When to use abstract classes and interfaces

Source: Internet
Author: User

"An interface is a fully abstract set of members, and its members cannot be implemented when the interface is defined. We can regard it as a contract for Operation definition, and the implementation of the interface is left for developers to do. There are many differences between them. If you carefully analyze them, there are still many: in Java, classes can only be inherited from a base class, so if you want to use abstract classes to provide polymorphism for a group of classes, these classes must all inherit from that class. interfaces are different. They can not only implement multiple interfaces with one class or structure, but also have multiple implementations for one interface."

An abstract class is a class that cannot be instantiated and must be inherited from it. Abstract classes can be fully implemented, but more commonly, they are partially implemented or not implemented at all, so as to encapsulate the general functions of the inherited class. It can provide the implemented members. Therefore, abstract classes can be used to ensure that a specified number of functions are the same, but interfaces cannot be used to do so. "That is to say, are there any differences between them on the issue of providing polymorphism ?" I seem to understand something. "If you want to create multiple versions of a component, you should create an abstract class. This is because abstract classes provide simple methods to control component versions. By updating the base class, all inheritance classes are automatically updated with the change. This is a benefit, but it is also a problem.On the other hand, the interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface. Therefore, if the created function is used across a wide range of different objects, the interface is used ." ------: "Can you say that abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes ." I told you in the morning that to create a control, the first thing to do is to implement some interfaces so that the system can recognize (For details, see Interface ). In fact, the relationship between various controls is not highly relevant. Therefore, most of them are interfaces. However, we should note that when designing a component, an abstract class is used to provide common implemented functions among all the implementations of the component. This is because the abstract class allows partial implementation classes, and the interface does not contain any member implementations ."

"Well, I understand the differences between them ." I nodded silently.

"In addition, there is a general design concept. If you want to design small and concise functional blocks, you can use interfaces. If you want to design a large functional unit, use an abstract class ." "It seems that the design problem is still quite big. In general, how to design interfaces ?" I went on to ask.

"Why do you see Programming books,ProgramThere are very few interface descriptions, and examples of class implementation inheritance are everywhere? This reminds us of one aspect. If appropriate, interfaces can become very useful tools. However, if improperly used, they become very tricky and even impede effective programming. The design and use of interfaces is actually a brilliant art ."

,"Through the interface and implementation method, we can apply the same type of program to different objects without modifying the original class. The relative subroutine must modify the source programCodeIn order to achieve the purpose of reuse, interface and implementation is not only a great progress, but also a very high state of the art of programming ."

"However, the biggest problem is the interface design. Once an interface is defined and accepted, it must remain unchanged to protect applications written to use the interface. After the API is released, it cannot be changed. This is an important principle for component design. It is called 'interface immutability '."

"I have already repeatedly stressed that creating an interface is to create a definition, which cannot be changed once released. The immutability of interfaces is to protect existing systems written to use interfaces. When there are differences between the interface design and requirements and the need for significant changes, we should create a new interface. The general naming method is that the new interface can append a number after the original interface name'2 'to show its relationship with the existing interface. Then create an interface through interface inheritance ."

"However, if the demand analysis is poor, wouldn't there be a lot of derivative interfaces ?" I have some concerns.

"This is for sure, and too frequently new interfaces are generated, which makes the components very large because of unused interface implementations. Experienced designers, after fully analyzing the requirements, the interfaces designed are often very small and independent from each other, reducing the possibility of performance problems ."

"This decomposition capability is really art !" I can't help but sigh for it.

"Of course, in general, we call the design process of determining which attributes and Methods belong to an interface as 'interface decomposition '. The basic principle is to aggregate closely related functions into a group in an interface. Too many functions make the interface not easy to run, and too many subdivided functions will lead to additional system overhead and reduce ease of use. The ability to master the degree of decomposition requires constant technical refining and in-depth analysis and understanding of each project ."

What are the advantages of interfaces over class implementation inheritance ?" : "Let me try it out. In general, interfaces are a very effective programming method, which separates object definitions from implementations, so that objects can be improved and evolved without disrupting existing applications. The interface eliminates a major problem of implementation inheritance, that is, the Code may be damaged when the design is modified. Even if the implementation inheritance allows the class to inherit from the base class, we still have to make a lot of choices for the design at the first release of the class. If the original idea is incorrect, it is not always possible to make security changes to the Code in a later version. For example, we define a base class method, which requiresThe integer parameter, which is determined later to be of the long data type. We cannot safely change the original class because the applications designed for Classes derived from the original class may not be able to compile correctly. This problem will be extended because a single base class will affect hundreds of sub-classes ."

"That's to use to reload the original class and useCan long parameters solve this problem ?" David raised a question.

"This ?" I thought for a moment, "however, this may not necessarily achieve satisfactory results, because a derived class may need to override the integer method. Long The method of the data type cannot be overwritten, and the derived class may not run properly ."

"How to Use interfaces ?" David continued to ask.

"The solution is to publish an interface for accepting new data types ." I answered it all at once.

"It seems that you have mastered the basic steps of interface operations ." I am so happy with the comments from Da li. "Let me summarize for you,The main reasons for using interface inheritance instead of class inheritance are: when the application requires many object types that may not be relevant to provide certain functions, the interface will be more adaptable; interfaces are more flexible than basic classes because you can define a single implementation to implement multiple interfaces. interfaces are better without inheriting from the base class; the interface is useful when class inheritance cannot be used. For example, structures cannot be inherited from classes, but they can implement interfaces ."

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.