English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Java 8Previously, interfaces could have constant variables and abstract methods.
We cannot provide method implementations in interfaces. If we want to provide a combination of abstract methods and non-abstract methods (methods with implementations), then we must use an abstract class.
public class Tester { 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 execution output of the above example is as follows:
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 the above example, each logging method has its own implementation.
In Java 8 The interface introduces some new features - default methods and static methods. We can use them in Java SE 8Methods are implemented in the interface by writing methods, and it only requires the use of the default keyword to define them.
In Java 8 In an interface, you can define the following types of variables/Method:
Constant
Abstract method
Default method
Static method
public class Tester { 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 execution output of the above example is as follows:
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
Java 9 Not only like Java 8 supports interface default methods as well as private methods.
In Java 9 In an interface, you can define the following types of variables/Method:
Constant
Abstract method
Default method
Static method
Private method
Private static method
The following example extracts redundancy into general methods, making it look obviously more concise:
public class Tester { 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 execution output of the above example is as follows:
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