In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the new features of Java8". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Before Java 8, by default, all methods in an interface were public and abstract. But this limitation was broken in Java 8, which allows developers to add new methods to interfaces without making any changes in the classes that implement those interfaces.
Why is there a default method?
Mainly to facilitate the expansion of existing interfaces; if there is no default method, if you add a new abstract method to an interface in Java, then all classes that implement that interface will have to be modified, which will have a great impact.
For example, the Sortable interface and the classes SortableNumberCollection and SortableStringCollection that implement it. There are two methods for this interface: void sort () and T peek ().
Public interface Sortable {void sort (); T peek ();}
The sort () method is used to sort objects, T peek () is used to get the specified elements, and a comparator class ObjectComparator is required to sort the objects.
Public class ObjectComparator implements Comparator {@ Override public int compare (Comparable o1, Comparable O2) {return o1.compareTo (O2);}}
SortableStringCollection is a custom collection class that can sort and view the string specified elements as follows:
Public class SortableStringCollection implements Sortable {private List items = new ArrayList (); public void add (String item) {items.add (item);} @ Override public void sort () {items.sort (new ObjectComparator ());} @ Override public String peek () {return items.get (0);}}
Similarly, SortableNumberCollection is a custom collection class that contains numeric list specified elements that can be sorted and viewed using interface methods, as follows:
Public class SortableNumberCollection implements Sortable {private List items = new ArrayList (); public void add (Integer item) {items.add (item);} @ Override public void sort () {items.sort (new ObjectComparator ());} @ Override public Integer peek () {return items.get (0);}}
If you add a new method to interface Sortable before Java8: t sortAndPeek (), then SortableStringCollection and
SortableNumberCollection must implement the T sortAndPeek () method.
After Java8, a new implementation is provided. The default method is default method. We can modify Sortable as follows:
Public interface Sortable {void sort (); T peek (); default T sortAndPeek () {/ / New 'default method' added in the interface sort (); return peek ();}}
At the same time, the SortableStringCollection and SortableNumberCollection classes do not need any changes. This will reduce our changes to the original code. You can also override the default implementation of the method T sortAndPeek () in any class that implements this interface, if desired.
In the following figure, we see the logo that default Method is not available:
Problems with using default methods in multiple inheritance
If two or more interfaces have the same default method signature, and a class implements both interfaces, a compile-time error is thrown. For example:
Public interface Interface1 {void methodOne (String str); default void newMethod () {System.out.println ("Interface1: Newly added method");}} public interface Interface2 {void methodTwo (String str); default void newMethod () {System.out.println ("Interface2: Newly added method") } public class InterfaceImplementation implements Interface1, Interface2 {@ Override public void methodOne (String str) {System.out.println ("Overridden methodOne:" + str);} @ Override public void methodTwo (String str) {System.out.println ("Overridden methodTwo:"+ str);}}
This time code prompts the following exception:
InterfaceImplementation inherits unrelated defaults for newMethod () from types Interface1 and Interface2
To solve this problem, we will have to override the methods in the class InterfaceImplementation:
Public class InterfaceImplementation implements Interface1, Interface2 {@ Override public void methodOne (String str) {System.out.println ("Overridden methodOne:" + str);} / / newMethod implemented to resolve the conflict. @ Override public void newMethod () {System.out.println ("InterfaceImplementation: Newly added method");} @ Override public void methodTwo (String str) {System.out.println ("Overridden methodTwo:" + str);}}
Let's sum up:
The methods in the class have the highest priority. The method declared in the class or parent class takes precedence over any method declared as the default method.
If it cannot be judged by the first rule, then the priority of the subinterface is higher: when the function signature is the same, the interface with the most specific default method is preferred, that is, if B inherits A, then B is more specific than A.
Finally, if it is still impossible to determine, a class that inherits multiple interfaces must explicitly choose which implementation of the default method to use by explicitly overriding and calling the desired method.
Add static methods in Java 8
The static method defined by the interface is independent of any object call. Therefore, when calling a static method, you do not need to implement the interface or an instance of the interface
Just like the default method, static methods can also be added to the interface. For example, we can add a static method Direction getDefaultDirection (), which returns the default Direction, for example:
Public interface Sortable {Direction defaultDirection = Direction.DESC; enum Direction {ASC, DESC}; void sort (); T peek (); static Direction getDefaultDirection () {/ / 'static method' added to the interface. Return defaultDirection;}}
In the above example, you can use a class reference to call the static Direction getDefaultDirection () method:
Sortable.getDefaultDirection ()
Some thoughts on default method and static method
The interface is an experience of the open-close principle in the design pattern, and java8 gives new features to the interface, which makes the interface easier to use, which also helps us to cohesion our own code structure. There are also many scenarios in the Java source code that use the default method, such as the Iterator interface. We can use some new features in the development to improve the development efficiency and increase the robustness of the code.
Public interface Iterable {Iterator iterator (); default void forEach (Consumer
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.