What is the role of the Java interface?

Source: Internet
Author: User
Tags emit

I think maybe I'm not the only one who has this question, so put it out.

here are the answers given to me by Madai and Clkrst. Thank you for two people here.   my question is: What does the Java interface do ?   1, read from the book is the Java Program interface only defines the 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 in the same class to write the method body, which is related to multiple inheritance.   3, in the package of those defined interfaces, how to know where the surface defines what method.   in the final analysis, the interface has any practical significance, which book does not speak, blindly speaking is more inheritance, not the actual reality is not good understanding.   Madai 's understanding is that :   the use of Java interfaces is mainly reflected in the following aspects:  (1) The same behavior of unrelated classes can be realized through an interface, without needing to know the class corresponding to the object.    (2) through an interface you can specify the methods that multiple classes need to implement.   (3) interfaces can be used to understand the interface of an object, without needing to know the class that the object corresponds to.   Clkrst gives a more detailed explanation:  class describes an entity, including the state of an entity, and also the actions that an entity may emit.   an interface defines an action 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, the interface is the function declaration, and the class is the function implementation. It is important to note that the same declaration may have many implementations.   so as for your question:  1, the interface defines the prototype of the class method, but can not be said to be empty method, because the meaning of the empty method is the implementation of the 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 certainly write the method directly in the class without the interface, but if you have a set of methods that need to be implemented in many classes, then abstracting them out and making an interface specification is not a good thing.   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 a certain set of actions, interface B defines one of the other sets of actions, such a structure, relatively clear.    This approach has the advantage of multiple inheritance and avoids the defects of multiple inheritance. In fact, in history, the interface has been designed to solve the problems caused by multiple inheritance to a large extent.   3, in the package of those defined interfaces, how to know where the surface defines what method.   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?

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.