Interfaces can be inherited (extends ).

Source: Internet
Author: User

In traditional textbooks or books, we will instill a concept that abstract classes can be inherited (extends), interfaces can be implemented (implements), but abstract classes cannot be implemented (implements ), the interface cannot be inherited (extends), but today's point of view has been overturned.

Abstract classes can be inherited (extends), interfaces can be implemented (implements), but abstract classes cannot be implemented (implements). There are no problems with these three ideas, but the interface can be inherited. Of course, you may give an example similar to the following to prove that the interface cannot be inherited,CodeAs follows:

Indeed, the above Code cannot be compiled and the following error occurs:

You may want to use the above example to prove that my opinion is incorrect, but please refer to the following example:

Look at the highlighted part of the code in the figure. It can be compiled. The following compilation result:

In addition, I also did tests in C #, because the implementation and inheritance in C # cannot be seen from the implements or extends keywords similar to Java. They are all reflected by the: symbol, however, you can see the specific code. See the following code:

Using System;
Using System. Collections. Generic;
Using system. text;

namespace day2
{< br>
interface interfacea
{< br>
int Add ();
}

interface interfaceb: interfacea
{< br>
int minus ();
}< BR >}< br>

the code can be compiled in.

the preceding example shows that interfaces can be inherited, that is, interface expansion, in this way, the class that inherits an interface has two interface methods.

conclusion: interfaces can inherit interfaces, and classes can only implement interfaces.

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.