A detailed description of the default method for Java 8
Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs
Java 8 Adds the default method, which can add new feature features to an interface without affecting the implementation class of the interface. Let's illustrate this by example.
public class MyClass implements Interfacea {public static void main (string[] args) {} @Overridepublic void saysomething () {/ /TODO auto-generated method stub}}interface interfacea{public void SaySomething ();}
The above code shows that the MyClass class implements the SaySomething () method of the Interfacesa interface. Now we have a new Sayhi () method for the Interfacesa interface. In doing so, the MyClass class is not compiled unless we provide the implementation of the Sayhi ().
The default method is very useful, and the implementation class does not need to provide the implementation of the method by adding the keyword default before the access modifier of the method defined by the interface. Like what:
public class MyClass implements Interfacea {public static void main (string[] args) {} @Overridepublic void saysomething () {/ /TODO auto-generated method stub}}interface interfacea{public void saysomething ();d efault public void Sayhi () { System.out.println ("Hi");}}
Note that we must provide the implementation of all default methods. Therefore, the default method makes our code more flexible and can be written in the interface. The implemented method is implemented as the default method.
So what if there is a conflict between the multiple interfaces?
Because Java classes can implement multiple interfaces, there may be situations where two or more interfaces have a default interface method with the same name, which creates a conflict. Because a Java virtual machine is not clear as to which default method you want to use while the program is running. This can cause compilation errors.
Let's take a look at the following example.
public class MyClass implements Interfacea, Interfaceb {public static void main (string[] args) {MyClass mc = new MyClass (); Mc.sayhi ();} @Overridepublic void SaySomething () {//TODO auto-generated method stub}}interface interfacea{public void saysomething () ;d efault public void Sayhi () {System.out.println (' Hi from Interfacea ');}} Interface Interfaceb{default public void Sayhi () {System.out.println (' Hi from Interfaceb ');}}
It is not compiled and will report the following error:
"Duplicate default methods named Sayhi with the parameters () and () is inherited from the types Interfaceb and Interface A. "
Unless the Sayhi () method is overridden in the MyClass class:
public class MyClass implements Interfacea, Interfaceb {public static void main (string[] args) {MyClass mc = new MyClass (); Mc.sayhi ();} @Overridepublic void SaySomething () {//TODO auto-generated method stub} @Overridepublic void Sayhi () {System.out.println ("Implemetation of Sayhi () in MyClass");}} Interface interfacea{public void saysomething ();d efault public void Sayhi () {System.out.println (' Hi from Interfacea ');}} Interface Interfaceb{default public void Sayhi () {System.out.println (' Hi from Interfaceb ');}}
If you want to specify which interface's Sayhi () method to call, we can do this:
public class MyClass implements Interfacea, Interfaceb {public static void main (string[] args) {MyClass mc = new MyClass (); Mc.sayhi ();} @Overridepublic void SaySomething () {//TODO auto-generated method stub} @Overridepublic void Sayhi () { InterfaceA.super.sayHi ();}} Interface interfacea{public void saysomething ();d efault public void Sayhi () {System.out.println (' Hi from Interfacea ');}} Interface Interfaceb{default public void Sayhi () {System.out.println (' Hi from Interfaceb ');}}
Is the answer not very simple?
A detailed description of the default method for Java 8