Java 9 Private Methods in Interface
This article will help you to understand “java 9 private methods in interface.”
Java 9 is going to release with Private Method in Interface. Let’s go through the history of interface till Java 9.
Interface | Java 7
In Java SE 7 or earlier versions, an interface can have two kinds of feature.
- Constant Variables
- Abstract Methods
0 1 2 3 4 5 6 |
public interface Java7Interface { String JAVA7_CONSTANTS = "Java7"; public abstract void printLine(String printStr); } |
These interface methods MUST be implemented by classes which choose to implement the interface.
Limitations: We can not provide implementation of any method in the interface. There is not any resolution with Java 7. We must go to abstract classes.
Inteface | Java 8
To overcome limitations for Java 7, Java 8 introduces Default methods and Static methods.
We can write method implementations in Interface from Java SE 8 and onwards. We need to use
In Java SE 8 and later versions, an interface have two more features.
- Constant Variables
- Abstract Methods
- Default Methods
- Static Methods
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public interface Java8Interface { String JAVA7_CONSTANTS = "Java7"; public abstract void printLine(String printStr); public default void printDefaultContent(String defaultContent) { System.out.println("Default Contents pasted if called."); } public static double calculateInterest(double amount) { return amount * 0.25; } } |
Limitations: If we want to provide a common method for all implementation method which we do not want to expose it outside. We must have to declare a common method as public. It increases redundancy.
Inteface | Java 9
Java 9 has the perfect solution for all above limitations.
We can write private methods in Interfaces using
In Java SE 9 and later versions, an interface have two more features.
- Constant Variables
- Abstract Methods
- Default Methods
- Static Methods
- Private Methods
- Private Static Methods
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public interface Java9Interface { String JAVA7_CONSTANTS = "Java7"; public abstract void printLine(String printStr); public default void printDefaultContent(String defaultContent) { System.out.println("Default Contents pasted if called."); } public static double calculateInterest(double amount) { return amount * 0.25; } private void printInterLogging(String message){ System.out.println("Method is used to print internal logging."); } } |
There is a difference between
private method is for fully implemented method. Sub Class cannot inherit and override private methods.abstract method is for no-implementation method. Sub Class should inherit and override private methods.
Private methods are useful or accessible only within that interface only. We cannot access or inherit private methods from an interface to another interface or class.
Private methods can be static or instance. In both cases, the private method is not inherited by sub-interfaces or implementations.
That’s it all about Private Methods in Interface – Java 9. Please visit 8 New Features of Java 9 for more Java 9 features.
This is good information