How does java-7.5 inherit the extended interfaces from the ground up?

Source: Internet
Author: User

How does java-7.5 inherit the extended interfaces from the ground up?

This chapter discusses how to inherit the extended interfaces.

An interface has the same inheritance property as a class, but it only 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 void sleep() {// TODO Auto-generated method stub}@Overridepublic void sing() {// TODO Auto-generated method stub}}


From the code above, we can see that WhatSingerCanDo inherits WhatPersonCanDo, so that Singer needs to override three methods when implementing the WhatSingerCanDo interface.

 

Although interfaces can be extended through inheritance, note that different interfaces define methods with the same name.

Because different interfaces define methods with the same label, and they seem to be overwritten, this will greatly reduce readability.

Of course, this may be rare in actual programming, but it may also be worth attention.

The following is an error code. No matter how you comment it out, it is an error. The following comments describe some errors.

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 are incompatible for the inherited methods interface2.run(int), father.run(int) */class Test extends father implements interface1, interface2 {//error/** * Multiple markers at this line - The return type is incompatible with * father.run(int) - overrides com.ray.ch07.father.run - Duplicate method * run(int) in type Test */// @Override// public void run(int speed) {// }/** * Multiple markers at this line - The return type is incompatible with * interface2.run(int) - overrides com.ray.ch07.father.run */// @Override// public int run(int speed) {// }@Overridepublic void run() {}}

 

Of course, the above situation is rare, because most programmers now use the framework, and they all use single inheritance and single interface, but we must pay attention to this.

 

 

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.