Name Conflict and combination interface conflict

Source: Internet
Author: User

Name Conflict and combination interface conflict

I have never noticed that there may be method conflicts when implementing multiple interfaces. I saw an interesting example in Thinking in Java, which is essentially a problem of overloading and rewriting, for more information about the concepts and differences between Overloading and rewriting, see another article on Java basics 1: Object-oriented features. First, let's look at the example:

1 interface I1 {2 void f (); 3} 4 5 interface I2 {6 int f (int I); 7} 8 9 interface I3 {10 int f (); 11} 12 13 class C {14 public int f () {15 return 1; 16} 17} 18 19 class C2 implements I1, i2 {// reload 20 @ Override21 public void f () {} 22 23 @ Override24 public int f (int I) {25 return 0; 26} 27} 28 29 class C3 extends C implements I2 {30 @ Override31 public int f (int I) {// overload 32 return 0; 33} 34} 35 36 class C4 extends C implements I3 {37 @ Override38 public int f () {// rewrite 39 return 0; 40} 41} 42 43 // class C5 extends C implements I1 {} 44 // 45 // interface I4 extends I1, i3 {} 46 47 public static void main (String [] args) {48 MethodCollision methodCollision = new MethodCollision (); 49 C4 c4 = methodCollision. new C4 (); 50 System. out. println (c4.f (); // output 051}

The method in interface I1 exists.Void f (), I2 existsInt f (int I), I3 existsInt f (), Class C existsInt f ().

Class C2 implementation interface I1 and I2, the implementation methodVoid f ()AndInt f (int I)Different signatures are reloaded;

Class C3 implementation interface I2, inherit C, C3 implement in class I2Int f (int I ),And inherit fromInt f ()Method, which has different signatures and is a heavy load;

C4 class implements interface I3, inherits C, C4 implementsInt f (),And inherit fromInt f ()Method, which has the same signature and the return value type is the same, which is an overwrite (0 output from the program can be described );

The C5 class implements the I1 interface and inherits C. Theoretically, the C5 class implements and inherits the I1 method respectively.Void f ()And C methodsInt f (), They have the same signature, different return value types, neither rewriting nor overloading, so the compiler prompts an error;

The I4 interface inherits I1 and I3, and theoreticallyVoid f ()AndInt f ()But it is neither a rewrite nor a reload. The Compiler reports an error.

Another interesting problem is that if I setVoid f ()ChangeIntF (), What will happen?

1 interface I1 {2 int f (); 3} 4 5 interface I2 {6 int f (int I); 7} 8 9 interface I3 {10 int f (); 11} 12 13 class C {14 public int f () {15 return 1; 16} 17} 18 19 class C2 implements I1, i2 {// reload 20 @ Override21 public int f () {22 return 0; 23} 24 25 @ Override26 public int f (int I) {27 return 0; 28} 29} 30 31 class C3 extends C implements I2 {32 @ Override33 public int f (int I) {// reload 34 return 0; 35} 36} 37 38 class C4 extends C implements I3 {39 @ Override40 public int f () {// rewrite 41 return 0; 42} 43} 44 45 class C5 extends C implements I1 {} // do not rewrite this function. The method inherited from C is to implement 46 47 interface I4 extends I1, i3 {} 48 49 class C6 implements I4 {// The I1 and I3 methods are the same. The implementation class only implements one 50 @ Override51 public int f () {52 return 0; 53} 54} 55 56 public static void main (String [] args) {57 MethodCollision methodCollision = new MethodCollision (); 58 C4 c4 = methodCollision. new C4 (); 59 System. out. println (c4.f (); // 060 61 C5 c5 = methodCollision. new C5 (); 62 System. out. println (c5.f (); // 163}

The method in interface I1 exists.Int f (), I2 existsInt f (int I), I3 existsInt f (), Class C existsInt f ().

Class C2 implementation interface I1 and I2, the implementation methodInt f ()AndInt f (int I)Different signatures are reloaded;

Class C3 implementation interface I2, inherit from Class C, which is a heavy load;

C4 class implementation interface I3, inherit from C, is a rewrite (from program output 0 can be described );

The C5 class implements the I1 interface, inherits C, and the C5 class implements and inherits the I1 method respectively.Int f ()And C methodsInt f ()Which is the same as that of the C4 class. Is it a rewrite? No, because the function inherited from C has implemented this interface and is not overwritten (output from program 1 can be explained );

The I4 interface inherits I1 and I3, and The I1 and I3 InterfacesInt f (), They have the same method signature and return value type, so I4 hasInt f ()(You can see through the C6 implementation class );

Class C6 implementation interface I4 with an implementation functionInt f ().

Summary:
  • The class cannot be inherited multiple times, and the interface can be inherited multiple times;
  • When combining interfaces, methods may be overloaded and overwritten, and code confusion may be introduced to reduce code readability, so avoid this situation as much as possible;
  • In this example, you can learn the concepts of inheritance, interface implementation, overloading, and rewriting. One of them is the class inheritance method or the implementation method of the interface.

 

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.