How to use private methods of interfaces in Java9
Previous version of Java 8
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
JDK 8 version
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
Java 9 version
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
