Before Java 8, the interface seems to only allow two types of data, one is a constant, the other is a public (public) virtual method (abstract), and it is a virtual method Oh, there is no implementation method, because these methods must be implemented by classes. In other words, there are no methods with default implementation in versions before Java 8.
Let’s take a look at an example,
public class InterfacePrivateMethodTester { public static void main(String []args) { LogOracle log = new LogOracle(); log.logInfo(""); log.logWarn(""); log.logError(""); log.logFatal(""); LogMySql log1 = new LogMySql(); log1.logInfo(""); log1.logWarn(""); log1.logError(""); log1.logFatal(""); } } final class LogOracle implements Logging { @Override public void logInfo(String message) { getConnection(); System.out.println("Log Message : " + "INFO"); closeConnection(); } @Override public void logWarn(String message) { getConnection(); System.out.println("Log Message : " + "WARN"); closeConnection(); } @Override public void logError(String message) { getConnection(); System.out.println("Log Message : " + "ERROR"); closeConnection(); } @Override public void logFatal(String message) { getConnection(); System.out.println("Log Message : " + "FATAL"); closeConnection(); } @Override public void getConnection() { System.out.println("Open Database connection"); } @Override public void closeConnection() { System.out.println("Close Database connection"); } } final class LogMySql implements Logging { @Override public void logInfo(String message) { getConnection(); System.out.println("Log Message : " + "INFO"); closeConnection(); } @Override public void logWarn(String message) { getConnection(); System.out.println("Log Message : " + "WARN"); closeConnection(); } @Override public void logError(String message) { getConnection(); System.out.println("Log Message : " + "ERROR"); closeConnection(); } @Override public void logFatal(String message) { getConnection(); System.out.println("Log Message : " + "FATAL"); closeConnection(); } @Override public void getConnection() { System.out.println("Open Database connection"); } @Override public void closeConnection() { System.out.println("Close Database connection"); } } interface Logging { String ORACLE = "Oracle_Database"; String MYSQL = "MySql_Database"; void logInfo(String message); void logWarn(String message); void logError(String message); void logFatal(String message); void getConnection(); void closeConnection(); }
The running results are as follows
$ javac InterfacePrivateMethodTester.java && java InterfacePrivateMethodTester
Open Database connection
Log Message : INFO
Close Database connection
Open Database connection
Log Message : WARN
Close Database connection
Open Database connection
Log Message : ERROR
Close Database connection
Open Database connection
Log Message : FATAL
Close Database connection
Open Database connection
Log Message : INFO
Close Database connection
Open Database connection
Log Message : WARN
Close Database connection
Open Database connection
Log Message : ERROR
Close Database connection
Open Database connection
Log Message : FATAL
Close Database connection
In this example, each type of log has its own implementation
What is the biggest pitfall? Each method must be implemented once. Yes, each method must implement
And Java 8 has finally made some changes. The interface in Java 8 can have the following types of variables and methods
1. Constant
2. Virtual method
3. Default method
4. Static method
Let’s change the above example , using the features of Java 8
public class InterfacePrivateMethodTester { public static void main(String []args) { LogOracle log = new LogOracle(); log.logInfo(""); log.logWarn(""); log.logError(""); log.logFatal(""); LogMySql log1 = new LogMySql(); log1.logInfo(""); log1.logWarn(""); log1.logError(""); log1.logFatal(""); } } final class LogOracle implements Logging {} final class LogMySql implements Logging {} interface Logging { String ORACLE = "Oracle_Database"; String MYSQL = "MySql_Database"; default void logInfo(String message) { getConnection(); System.out.println("Log Message : " + "INFO"); closeConnection(); } default void logWarn(String message) { getConnection(); System.out.println("Log Message : " + "WARN"); closeConnection(); } default void logError(String message) { getConnection(); System.out.println("Log Message : " + "ERROR"); closeConnection(); } default void logFatal(String message) { getConnection(); System.out.println("Log Message : " + "FATAL"); closeConnection(); } static void getConnection() { System.out.println("Open Database connection"); } static void closeConnection() { System.out.println("Close Database connection"); } }
The running results are as follows
$ javac InterfacePrivateMethodTester.java && java InterfacePrivateMethodTester
Open Database connection
Log Message: INFO
Close Database connection
Open Database connection
Log Message : WARN
Close Database connection
Open Database connection
Log Message : ERROR
Close Database connection
Open Database connection
Log Message : FATAL
Close Database connection
Open Database connection
Log Message : INFO
Close Database connection
Open Database connection
Log Message : WARN
Close Database connection
Open Database connection
Log Message: ERROR
Close Database connection
Open Database connection
Log Message: FATAL
Close Database connection
Because in the interface of Java 8 Methods can have default implementations, that is, methods modified with the default
keyword
. Therefore, it is relatively simple for a class to implement a certain interface, and some methods can be selectively implemented.
However, there is still a pitfall, that is, the code in each default method must be complete, and other default implementation methods cannot be called
Finally I can't bear it anymore. Java 9 can provide private methods for interfaces, including private member methods and private static methods
So the interface in Java 9 can have the following types of variables and methods
1 , Constant
2, Virtual method
3, Default method
4, Static method
5, Private static method
6 , Private method
So, we can continue to modify the instance just now, making it simpler and clearer
InterfacePrivateMethodTester.java
public class InterfacePrivateMethodTester { public static void main(String []args) { LogOracle log = new LogOracle(); log.logInfo(""); log.logWarn(""); log.logError(""); log.logFatal(""); LogMySql log1 = new LogMySql(); log1.logInfo(""); log1.logWarn(""); log1.logError(""); log1.logFatal(""); } } final class LogOracle implements Logging {} final class LogMySql implements Logging {} interface Logging { String ORACLE = "Oracle_Database"; String MYSQL = "MySql_Database"; private void log(String message, String prefix) { getConnection(); System.out.println("Log Message : " + prefix); closeConnection(); } default void logInfo(String message) { log(message, "INFO"); } default void logWarn(String message) { log(message, "WARN"); } default void logError(String message) { log(message, "ERROR"); } default void logFatal(String message) { log(message, "FATAL"); } private static void getConnection() { System.out.println("Open Database connection"); } private static void closeConnection() { System.out.println("Close Database connection"); } }
The running results are as follows
$ javac InterfacePrivateMethodTester.java && java InterfacePrivateMethodTester
Open Database connection
Log Message : INFO
Close Database connection
Open Database connection
Log Message : WARN
Close Database connection
Open Database connection
Log Message : ERROR
Close Database connection
Open Database connection
Log Message : FATAL
Close Database connection
Open Database connection
Log Message : INFO
Close Database connection
Open Database connection
Log Message : WARN
Close Database connection
Open Database connection
Log Message : ERROR
Close Database connection
Open Database connection
Log Message : FATAL
Close Database connection
The above is the detailed content of How to use private methods of interfaces in Java9. For more information, please follow other related articles on the PHP Chinese website!