java-Preliminary Understanding-Nineth-interface-multi-implementation

Source: Internet
Author: User

One.

Does the interface appear just to re-write the special abstract class? is the abstract method defined as an interface, only this benefit?
Just because of a special abstract class?

No, the appearance of the interface solves a lot of practical problems. For example, when I was studying, I said that Java does not support multiple inheritance. Multiple inheritance is a subclass that has more than one parent class.

uncertainty means that the inherited multiple parent classes may have the same function (the same is said for the same function declaration, the content can be the same, or not the same).

What is the purpose of multi-implementation?
The purpose of multi-inheritance is to improve the function of subclasses, and the extensibility has been improved.

In order to extend the functionality of subclasses, Java is improved into multiple implementations. There are two problems here, and there is no uncertainty about how to achieve them ? interfaces are abstract methods, implementing a multi-implementation structure to rewrite, meaning ?

The second problem is that you want to override the method, and the multi-implementation solves the fact that the subclass of the implementation has some functionality, and the trouble with overriding the function is not within the scope of consideration. So the first question of uncertainty?

Multiple inheritance is not supported, because the method is nondeterministic, the declaration is the same, the method body is different , the subclass does not know which method body to execute when inheriting the same method. The problem is on the method body now. The use of multiple inheritance is forbidden to prevent such problems. But the interface is defined by all abstract methods, in multiple inheritance (the argument is not correct, to say that the implementation, but the essence or inheritance cover), even if encountered the same function, it does not matter, because there is no method body, to execute the method body is a subclass of its own rewrite, There is no choice to perform multiple different method bodies at all. Moreover, I think that in the multi-interface, the same method will not appear at all, because, when writing is written from the top down. It is only at the beginning of the understanding that it is from the bottom up that you may encounter these problems. Multiple inheritance does not execute, there is a general function in the parent class, not an abstract function problem. It's not that the subclass inherits several parent classes, and there are some things that can be done, but it's forbidden to prevent problems.

Interface is to improve some of the functions of subclasses, tell subclasses you can have what functions, as to how to implement these functions, you need to write the sub-class.

Two. How reality operates

is a multi-implementation of the specific operation, of course, the process omitted something, such as the structure of the method omitted abstract.

In the above, two interfaces tell the test class that you have the show () method, which does not conflict, which is equivalent to saying two times, but this is not the case in practice. Because, when writing, it is written from the top down.

There is no limit to how many implementations can implement the number of interfaces.

Now the two methods in the interface have been changed

In the program, the runtime will error, the problem occurs when the object is called on the child class, the object call method does not display the return value type, only display the method name, so there will be confusion, resulting in an error.

When multiple inheritance occurs, the names of the methods in the two parent classes result in an error.

java-Preliminary Understanding-Nineth-interface-multi-implementation

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.