The role of interfaces in Java

Source: Internet
Author: User

Reproduced in: https://www.zhihu.com/question/20111251

Confusion: For example I have defined an interface, but I am inheriting this interface in the class to write the implementation of the interface, then I would rather directly in this class to write the implementation method is not more convenient, but also eliminate the definition interface? What is the role of the interface in Java?

Summary answer:

an interface is a specification
    • software development is mostly a collaborative work : Electrical appliances and sockets are different people to complete, with the interface we can split open dry, according to the interface to do things, the completion of their own can be easily integrated together. Each part of the test is also more convenient.
    • software needs to evolve : Today you use the bull's socket, after a year you may change a Siemens socket, do not have this set of national interface standards, fragmented, it is estimated that you can not change the socket. You think, every time we go to the United States on business, we have to bring a adapter, or kneel, more inconvenient Ah, because the interface specifications are different Ah! (These adapters you are not smelling a thick adapter mode of taste).
and what you say, direct write implementation of the way in a deterministic scenario can of course, do not involve the division of labor, change, testing convenience and other factors, of course, no need to interface. In general, for example, you don't have to get a Pojo getter and setter to interface and implement separation.

The interface can quickly separate the work content during the development process.

For example, when writing business logic, the caller needs a function, perhaps a database access, or a complex computation, but his work focuses on implementing business logic and does not want to separate the effort to do the underlying implementation, then he only needs to implement an interface, define the specification, and then proceed to his business logic code. And the implementation can be based on this interface specification, to do concrete implementation.
By using the interface, we can quickly separate the work content and achieve the goal of the team working in parallel.

In addition, if the specification is defined through the interface, then when you have multiple implementations of this function, you can simply implement this interface, then the implementation is quickly replaced, so that the code level can be completely separated.

summed up in a sentence: the interface or specification can be separated during the development process.

Interface (English: Interface), in the Java programming language is an abstract type (abstract type), which is used to require the class to implement the specified method, Enables different classes of objects to communicate using the same interface. Interfaces are usually declared with interface , which can contain only method signatures (methods Signature) and constant declarations (variable declarations contain static and final ), an interface does not contain the implementation of the method (defined only).

The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared abstract class . In addition, in Java, the interface type can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.


The interface is a sign.

For example, you have a holiday this year to travel to Hangzhou, play a morning, you are also a little hungry, suddenly see a shop in front, hanging on top of KFC, and then you know today lunch has fallen.

KFC is the interface, we see this interface, we know that the store will sell fried chicken legs (Implement Interface).

So for God's horse we're going to define an interface Nirvana, this shop can sell fried chicken leg AH (direct write implementation method), yes, this shop can sell fried chicken leg directly, but there is no KFC sign, we can not directly blunt rushed in to call the waiter to two fried chicken leg.

Or we're going to go in and ask, you don't sell fried chicken legs here, sell hamburgers, sell the holy generation (this is reflection). Obviously, the question of such a family is very troublesome (the reflection performance is very poor).
Either, we have to remember, 108th Zhongshan road sell fried chicken, Huangshan Road 45th sell fried Chicken (hard code), it is obvious that we have to remember a lot of things (code volume explosion), and, if there is a new shop selling fried chicken legs, we can not know (not conducive to expansion).


The role of interfaces in Java

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.