Implementing multiple interfaces and parent classes with the same method and implementing interfaces in the same way

Source: Internet
Author: User

Implementing multiple interfaces with the same method

This situation is still relatively rare, but the fact is that there is such a problem, so write down the solution:

Use inner classes:

InterfaceIntera {voidf ();}InterfaceInterb {voidf ();} Public classCImplementsIntera { Public voidf () {//Implement Interface Intera methodSystem.out.println ("A.F ()");}Private classImplbImplementsInterb { Public voidf () {//Implement Interface Intera methodSystem.out.println ("B.f ()");}} PublicInterb Getb () {return Newimplb ();}//Use Public Static voidMain (string[] args) {c C=NewC (); Intera a= C;//C realizes the A interface, can be directly upward transformationa.f (); Interb b=C.getb (); B.f ();}} 

In this way, the method of using different interfaces is separated, if the two interfaces are implemented directly and then implemented, then the methods in both interfaces are implemented.

Second, the parent class and the implementation interface have the same method

When the parent class has the same method as the implementation interface, for example (the filter implemented with HttpServlet has the same destroy method)

If the implementation method is not displayed and no syntax errors are found, then the inherited method implements the same name method in the interface.

If you need to partition, then you need to rewrite and add a method to use the Super keyword in the added method to call the parent class method, and the overriding method does not call the parent class method, then it is considered to be separated. .

Note: If the situation is more complex (such as more interfaces, there is also a parent class) to cross multiple identical methods .... It should be difficult for everyone to encounter such a situation ... and the interface design method is meaningful, there will not be so many names.

Implementing multiple interfaces and parent classes with the same method and implementing interfaces in the same way

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.