Abstract: 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 the Operation definition, and the implementation of the interface is completely 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, we must first implement some interfaces to enable the system to recognize them (For details, refer to interface above). 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, program routines, and very few descriptions of interfaces, 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 interfaces and implementations, we can apply the same type of programs to different objects without modifying the original class, compared with subprograms, you must modify the source code to achieve reuse. interfaces and implementations are not only great progress, but also an extremely high level of programming art."
        "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 be named by appending a number '2' after the original interface name 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 defined a base class method, which requires an integer parameter, and later confirmed that the parameter should 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 ."
        "Can this problem be solved if the original class is reloaded and a long parameter is used ?" 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 rewrite the method using integers, if a method of the long data type cannot be overwritten, 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. "I would like to sum up that the main reasons for using interface inheritance instead of class inheritance are: when the application requires a lot of irrelevant object types to provide some function, the interface is more adaptable. The interface is more flexible than the base class, because you can define a single implementation to implement multiple interfaces. The interface is better without inheriting the implementation 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 ."
 

When do I use interfaces? When can I use abstraction? Select a Java interface or an abstract class)

Http://blog.csdn.net/kingchaool/archive/2011/03/04/6223434.aspx

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.