Inheritance between interfaces in java

Source: Internet
Author: User

Recently, when I was reading some source code, I suddenly found something amazing. Its original form is as follows: in this line of code, BlockingDeque, BlockingQueue, and Deque are three interfaces. I was surprised to find this problem, because even in Thinking in Java, inter-interface inheritance is not mentioned. So I immediately submitted this question to stackoverflow. As discussed in stackoverflow, a class can only extends one parent class, but can implements multiple interfaces. Java uses the interface concept to replace multi-inheritance in C ++. At the same time, an interface can be extends multiple interfaces at the same time, but cannot implements any interfaces. Therefore, interfaces in Java support multi-inheritance. Then I did another experiment to verify that if multiple Parent and Child interfaces have multiple identical method declarations, and then when implementing this final interface, will the declaration of the same method conflict with each other during implementation? First, I wrote an interface: TestInterfaceA. java: Copy code 1 package com. peter. java. dsa. interfaces; 2 3 public interface TestInterfaceA {4 String pri_key = "guess what the private key is"; 5 6 int add (int x, int y ); 7 8 String encryt (byte [] result); 9 10 int get (); 11} copy the code note that three methods and a variable are declared, and then an interface is added: testInterfaceB. java: Copy code 1 package com. peter. java. dsa. interfaces; 2 3 public interface TestInterfaceB {4 String pub_key = "Guess what the public key is"; 5 6 int minus (int x, int y); 7 8 byte [] decryt (String src); 9 10 int get (); 11} the copy Code also declares three methods and one variable. Then, it defines an interface InterfaceMultiInheritance. java inherits the TestInterfaceA interface. java and TestInterfaceB. java: Copy code 1 package com. peter. java. dsa. interfaces; 2 3 public interface InterfaceMultiInheritance extends TestInterfaceA, 4 TestInterfaceB {5 int num = 1024; 6 7 double divide (int X, int y); 8 9 int get (); 10} the copy Code declares two methods and a variable. Note that in the three interfaces, there is a common method declaration: get (). This is the topic to be discussed. Finally, in a class InterfaceImplementTest. java implements the InterfaceMultiInheritance interface. java, source code: Copy code 1 package com. peter. java. dsa. common; 2 3 import com. peter. java. dsa. interfaces. interfaceMultiInheritance; 4 import com.sun.org. apache. xml. internal. security. exceptions. base64DecodingException; 5 import com.sun.org. apache. xml. internal. security. utils. base64; 6 7 public class InterfaceImplementTest implements InterfaceMult IInheritance {8 9 @ Override10 public int add (int x, int y) {11 // TODO Auto-generated method stub12 return x + y; 13} 14 15 @ Override16 public String encryt (byte [] result) {17 // TODO Auto-generated method stub18 return Base64.encode (result ); 19} 20 21 @ Override22 public int minus (int x, int y) {23 // TODO Auto-generated method stub24 return x-y; 25} 26 27 @ Override28 public byte [] decryt (String s Rc) {29 // TODO Auto-generated method stub30 try {31 return Base64.decode (src); 32} catch (Base64DecodingException e) {33 // TODO Auto-generated catch block34 e. printStackTrace (); 35} 36 return null; 37} 38 39 @ Override40 public double divide (int x, int y) {41 // TODO Auto-generated method stub42 return x/y; 43} 44 45 @ Override46 public int get () {47 // TODO Auto-generated method stub48 return num; 49} 50 51 public void print () {52 System. out. println ("The public key is:" + pub_key + "\ nThe private key is:" + pri_key); 53} 54 55} copy The code in this class, only one get method is implemented, and there is no conflict between multiple get methods. At the same time, if the get method in InterfaceMultiInheritance. java is deleted, only one get method is implemented and there is no conflict between multiple get methods. Therefore, we can draw a conclusion that when the compiler implements the interface, it will still check the interface InterfaceMultiInheritance. java, TestInterfaceA. java and TestInterfaceB. method declaration in java. If the latter two have a method declaration that conflicts with the former, the compiler only requires the class to implement the declaration of the former, and the same method declaration in the latter two will be automatically ignored. However, when only the latter two have the same method declaration, the compiler will implement one of them. Just like the compiler has a Set that specifically stores the method declaration, only the same method declaration is saved once in an interface with an inheritance relationship.

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.