Java tip [011]: Does Java support single inheritance only?

Source: Internet
Author: User

Java only supports class single inheritance, and inheritance between interfaces also uses the extends keyword, but interfaces support multi-inheritance, example:

Interface IP1 {<br/>}</P> <p> interface ip2 {<br/>}</P> <p> Public interface isub extends IP1, ip2 {</P> <p >}< br/>

Obviously, the aboveCodeThere is no problem. Therefore, the title should be not rigorous. Strictly speaking, class inheritance in Java only supports single inheritance.

Of course, we will naturally think of the problem of multi-inheritance. What if the two parent interfaces have the same method?

Interface IP1 {<br/> Public void test (); <br/>}</P> <p> interface ip2 {<br/> Public void test (); <br/>}</P> <p> Public interface isub extends IP1, ip2 {</P> <p>}

In fact, there is no need to worry about this problem, because the interface is only a declaration of the method, and there is no specific implementation, so it is not important which parent interface the method in the sub-interface belongs, it is important that you only need to implement this method when implementing this interface. The implementation of this method should belong to two parent interfaces at the same time.

Obviously, this is not a real problem. The real problem is that if the names and parameters of the two parent interfaces are the same, the returned results are different:

Interface IP1 {<br/> Public void test (); <br/>}</P> <p> interface ip2 {<br/> Public String test (); <br/>}</P> <p> Public interface isub extends IP1, ip2 {</P> <p>}

This is the same as the problem. In this case, there will be a compilation error. The reason is very simple. The method overload can only be the same method name and different input parameters. For these two methods, they have the same method name and the same input parameters, but different return parameters cannot be used as overload methods. Therefore, for the compiler, This is a repeated definition of a method, obviously, it cannot be compiled.

Similarly, this problem also exists when a class implements multiple interfaces at the same time. Therefore, in these cases, we must note that the same method name exists, methods With the same input parameters cannot appear in the same class or interface.

 

Next article: Java tip [012]: Why class. forname ("") is used for JDBC Development ("")

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.