Meaning of the Java interface

Source: Internet
Author: User

Java to inherit an interface, to rewrite his method, then why should superfluous to implement an interface?

Write the method directly in the class is not possible?
That is, remove the implements interface after the class name, can not it?

The main function of the interface is to achieve unified access, is created when the object is created with the interface, "Interface Name" "Object Name" =new "Implementation of the interface class", so that you can use which class object to the new object, do not need to change the original code, and your USB interface, plug what read what, That's the principle. As you ask, there is a method1 method, if I use the interface, I can one.method1 (); So I'm new A (); The method of A, new B () is the method of B.
Isn't that convenient?
This is called unified access, because the method name of the class that implements the interface is the same, but the implementation content is different.
Another advantage of Java interfaces is that Java does not support multiple inheritance, but it can implement multiple interfaces, which can be seen as a way to carry out multiple inheritance in some way.

First of all, this is certainly not wrong, in our early semester time has not reflected the value of the interface. But when we are doing the project, it will be very convenient to use the interface. Because there is only a declaration of simple methods inside the interface. or something common. It is much easier for everyone to implement it than to redefine it every time.
For the role of the interface, on some small projects, it is difficult to see the advantages of its play. This makes some regular small project developers, do a long time to feel that it has nothing good, sometimes it is troublesome to write, simply do not have. In fact, in some large projects, the role of the interface is to play a fairly obvious.

For example: If you develop business logic code, when you finally achieve its full functionality, suddenly the user needs to change, you change your code at the same time, the other people calling your code will change, if the code relevance
Strong, a lot of people will have to change the code, so carefree matter, the program will become quite unstable, and there may be more new bugs, everyone can fall into chaos.

But if you use an interface, before you use it, think about all the functions it needs to implement (the interface is actually encapsulating the functionality). After determining the interface, if the user needs change, you just need to re-write it
Implementation class, and other people just call your interface, he doesn't matter how you do it, it only needs the functionality that the interface provides. In this way, you probably just need to modify your code so that no one else has to do anything.
At the same time: to do so, so that developers can clear division of labor, as long as the interface is determined, you can develop at the same time, improve development efficiency. In addition, the use of the interface is easy to use, readable, clear structure
and other advantages.

Defines an interface that can have multiple implementations. Variables are declared as interface variables, call interface methods, and later switch implementation classes, the original code does not have to be modified.

Decoupling, extensible this is one of the main reasons for designing interfaces

Why can interfaces inherit more, and classes not?

If there are two parent classes and two parent classes have the same method, how should you inherit this method as subclasses? Parent Class 1 or parent Class 2?
But implementing multiple interfaces is no problem, because no matter which interface, the invocation is the same implementation, because only the method name!
And one of the purposes of single inheritance is to reduce complexity and reduce maintenance difficulties.

Inheritance: Describes the natural properties of things and the reuse of behaviors.
Interface: Describes the social properties and the reuse of the behavior of a thing.

Because inheriting the parent class includes the implementation, the inherited interface only includes the interface, that is.
Interfaces define behavior, such as walking, and many objects can implement this behavior.
But the class defines the properties of the object and the specific behavior

Class is responsible for the implementation, interface is responsible for the interface; the most troublesome problem of multiple inheritance is the conflict, the conflict is mainly embodied in the timing and parameters of the implementation, the outgoing parameters in the implementation of the parent class timing problems, the language itself becomes extremely complex, And many inheritance problems in the implementation itself can be solved in many ways, and for the interface, the incoming parameter conflict is overload, it is not a problem, only the outgoing parameters this problem is the interface multi-inheritance is not allowed for example:

Public interface IA {
void DoSomething ();
}
Public interface IB {
Integer dosomething ();
}
Public interface IAB extends IA, IB {

@Override
public void dosomething ();

@Override
Public Integer dosomething ();
}

In this case the compiler will tell you that IA, IB interface conflict, is not allowed

Meaning of the Java interface

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.