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