What is the role of the Java interface? < turn >

Source: Internet
Author: User
Tags emit

  Here are the answers from Madai and Clkrst.    my question is, what does the:  java  interface do?    1, read from the book is the Java Program interface only defines constants and empty methods, what is the use of empty methods? Also want to write the method body in the class, that also want the interface what?   2, a program wrote 2 interfaces, and write the method body in the same class, which is related to multiple inheritance.   3, those defined interfaces in the package, how to know what the method is defined there.   After all, what is the actual meaning of the interface, which book does not speak, blindly speaking is more inheritance, not the actual reality is not good understanding.    Madai's understanding is: The use of Java interface is mainly embodied in the following aspects:  (1) through the interface can implement the same behavior of unrelated classes, without needing to know the class corresponding to the object.   (2) interfaces can be used to indicate how multiple classes need to be implemented.   (3) interfaces allow you to understand the interface of an object without needing to know the class that the object corresponds to.    Clkrst gives a more detailed explanation: The class describes an entity, including the state of the entity, as well as the actions that the entity may emit. The   interface defines the actions that an entity may emit. But only the prototypes of these actions are defined, not implemented, and there is no state information.   So the interface is a bit like a specification, a protocol, is an abstract concept, and the class is the implementation of this Protocol, satisfies the specification of the specific entity, is a specific concept.   from the point of view of the program, simple understanding, interface is a function declaration, class is a function implementation. It is important to note that the same declaration may have many implementations.   So on your question: 1, the interface defines the prototype of the class method, but not the empty method, because the empty method means that there is an implementation body, but the implementation of the body is empty operation. In fact, the interface does not define any implementation body. The specific implementations are in the class that implements the interface, and the interface simply defines how these methods are called.   You can of course not use the interface, directly in the class to write methods, but if your group of methods need to be implemented in many classes, then to abstract them out, to make an interface specification, is not better?   2, a class describes an entity, this entity may be a complex object, it is a lot of action, if you classify these actions, using interface A to define one of the set of actions, interface B defines one of the other set of actions, the structure is relatively clear.   This approach has the advantage of multiple inheritance, and avoids the defects of multiple inheritance. In fact, historically, interfacesTo a large extent, it is designed to solve the problems caused by multiple inheritance.   3, those defined interfaces in the package, how to know what the method is defined there. The   interface defines the input and output of the method, which are all protocols, and the implementation is in each class. For many places where you simply need to abstract an interface, you do not need to know what the specific class is, as long as this class implements this interface.

What is the role of the Java interface? < turn >

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.