In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to deeply analyze the default method of Java 8 and multi-inheritance, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article, without saying much, follow the editor to have a look.
One of the advantages of Java over C++, which was often discussed before, is that there is no problem of multi-inheritance in Java. Because the Java subclass can only extends a single parent class, although it can implement multiple interfaces, there are only abstract methods in the interface, the method body is empty, there is no concrete method implementation, and there will be no method conflict.
It has been said for a long time that since the release of Java 8 this year, default method can also be defined in the interface. The reason why the previous design is broken in the interface
The purpose of adding specific methods is to add new functions to the classes of thousands of existing Java class libraries, and there is no need to redesign them. For example, just in the Collection interface
Add default Stream stream (), the corresponding Set and List interfaces and their subclasses all contain this method, and you don't have to re-copy this method for each subclass.
This is a compromise design, and the problem is that it introduces the problem of multiple inheritance for Java. We know that interfaces can inherit interfaces, and classes can inherit classes and implement interfaces. What happens once the inherited class and the implemented interface have a method with the same signature? This article will explore multiple inheritance in various situations in order to clearly understand the rules of Java multi-inheritance.
Interface inherits multiple parent interfaces
Suppose there are three interfaces Interface A, Interface B, and Interface C, and the inheritance relationship is as follows:
+-+-+ | Interface A | | Interface B | +-^-+ | | | +-+ | Interface C | +-+ |
A String name B has the default method default String say (String name) with the same signature, and if interface C does not have a method called override, there will be a compilation error.
Interface A {default String say (String name) {return "hello" + name;}} interface B {default String say (String name) {return "hi" + name;}} interface C extends A Magi B {}
Error message:
C:/Lambda/src > javac-J-Duser.country=US com/colobu/lambda/chapter3/MultipleInheritance1.java com/colobu/lambda/chapter3/MultipleInheritance1.java:17: error: interface C inherits unrelated defaults for say (String) from types An and B static interface C extends from types An and B static interface C extends B {^ 1 error
We can override the override method in subinterface C so that the compilation does not go wrong:
Interface C extends A greet B {default String say (String name) {return "greet" + name;}}
Note that the method signature does not include the return value of the method, that is, the signatures of the two methods that return different values are the same. The following code compiles without error, because the default methods of An and B are different, and C implicitly inherits the two default methods.
Interface A {default void say (int name) {}} interface B {default void say (String name) {}} interface C extends A Magi B {}
But in some cases, even different signature methods are difficult to distinguish:
Interface A {default void say (int a) {System.out.println ("A");}} interface B {default void say (short a) {System.out.println ("B");}} interface C extends static class D implements C {} public static void main (String [] args) {D d = new D (); byte a = 1; d.say (a); / / B}
Java will choose the most appropriate method, see Java specification 15.12.2.5
Interface multi-layer inheritance
Let's take a look at the problem of multi-tier inheritance. The inheritance relationship is shown in the following figure. A2 inherits A1 and C inherits A2.
+-+ | Interface A1 | +-+-+ | Interface A2 | +-+-+ | | | | +-+ | Interface C | +-+ |
Based on our previous understanding of class inheritance, it is easy to know that C inherits the default methods of A2, including the default methods that are directly defined, the default methods that are overridden, and the default methods that implicitly inherit from the A1 interface.
Interface A {default void say (int a) {System.out.println ("A");} default void run () {System.out.println ("A.run");}} interface B extends A {default void say (int a) {System.out.println ("B");} default void play () {System.out.println ("B.play");}} interface C extends A10 B {}
Multi-layer and multi-inheritance
The above example is also an example of single inheritance. What about multiple inheritance in the following figure?
2 3 4 5 6 7 8 9 10 11 12 13 14 15 + + | Interface A1 | +-+ | | | | +-+ | Interface A2 | | Interface B | +-+ | | | +-+-^ | +-+ | Interface C | + | -+
If A2 and B have the same signature method, this is the same as the * example. If you don't want to make a compilation error, you can override the default method of the parent interface, or you can call the default method that specifies the parent interface:
Interface A1 {default void say (int a) {System.out.println ("A1");}} interface A2 extends A1 {} interface B {default void say (int a) {System.out.println ("B");}} interface C extends A2 int B {default void say (int a) {B.super.say (a);}}
More complex multi-tier and multi-inheritance
+-+ | Interface A1 | +-+-+ | ^ +-+ | | +-+-+ | Interface A2 | + -+-+ | ^-+ + | +-+ | Interface C | +-+
Interface A2 inherits A1, and interface C inherits A2 and A1. The code is as follows
Interface A1 {default void say () {System.out.println ("A1");}} interface A2 extends A1 {default void say () {System.out.println ("A2");}} interface C extends A2 A1 {} static class D implements C {} public static void main (String [] args) {D d = new D (); d.say ();}
The above code will not make a compilation error, run the output A2.
You can see that interface C implicitly inherits the method of the subinterface, which is the default method of subinterface A2.
Class inheritance
If the inheritance relationship types are all classes, there will be no problem of multiple inheritance because the class is still single inheritance.
Classes and interfaces are mixed
What happens if we replace one of the interfaces in the * example with a class?
+-+-+ | Interface A | | Class B | +-+ ^-+-- +-^ | | +-- -+-+ | Class C | +-+
The following code does not cause compilation errors:
Interface A {default void say () {System.out.println ("A");}} static class B {public void say () {System.out.println ("B");}} static class C extends B implements A {} public static void main (String [] args) {C c = new C (); c.say (); / / B}
Results output B.
You can see that the subclass inherits the method of the parent class first, and only inherits the default method of the interface if the parent class does not have a method with the same signature.
Conclusion
More complex inheritance relationships can be simplified to the above inheritance relationships.
Based on the above examples, the following conclusions can be drawn:
Class takes precedence over interface. If a subclass inherits a parent class and an interface have the same method implementation. Then the subclass inherits the method of the parent class
Methods in child types take precedence over methods in parent types.
If none of the above conditions are met, the method must be overridden / implemented, or declared as abstract.
The above is how to deeply analyze the default method of Java 8 and multi-inheritance. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.