Get to know java-7.5 from the beginning how to extend the interface through inheritance?

Source: Internet
Author: User

Our chapter discusses how to extend the interface through inheritance.

An interface, like a class, has inherited attributes, but he simply inherits the interface and does not inherit the class.

Package Com.ray.ch07;interface Whatpersoncando {void run (); void sleep ();} Interface Whatsingercando extends Whatpersoncando {void sing ();} Class Singer implements Whatsingercando {@Overridepublic void Run () {//TODO auto-generated method stub} @Overridepublic vo ID sleep () {//Todo auto-generated method stub} @Overridepublic void Sing () {//Todo auto-generated method stub}}


As can be seen from the above code, Whatsingercando inherited the Whatpersoncando, so that singer in the implementation of the Whatsingercando interface, the need to rewrite three methods.

Although an interface can be extended through inheritance, it is important to note that the different interfaces define the same name method as a confusing point.

Because different interfaces define the same label method, and look like overwriting overrides, this makes readability much less.

Of course, this may be a little bit rare in actual programming, but it is also a possible point of attention.

Here is a wrong code, no matter how the comment is an error, there are some errors in the comments below.

Package Com.ray.ch07;interface Interface1 {void run ();} Interface Interface2 {void run (int speed);} Class Father {public int run (int speed) {return speed;}} /** * The return types is incompatible for the inherited methods Interface2.run (int), father.run (int) */class Test Extend s father implements Interface1, Interface2 {//error/** * Multiple markers at this line-the return type is incompatible W ITH * Father.run (int)-Overrides com.ray.ch07.father.run-duplicate method * Run (int) in type Test *///@Override//Publ IC void run (int speed) {//}/** * Multiple markers on this line-the return type is incompatible with * Interface2.run (in T)-Overrides Com.ray.ch07.father.run *///@Override//public int run (int speed) {//} @Overridepublic void Run () {}}


Of course, the situation above is very rare, because most programmers now use the framework, and are both single-inheritance and single-interface, but we must also pay attention to this.

This chapter is here, thank you.

-----------------------------------

Directory


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Get to know java-7.5 from the beginning how to extend the interface through inheritance?

Related Article

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.