Private methods in Interface | Java 9
In this article, we will see another JDK9 feature i.e. Private methods in Interface.
Everybody knows how humans have evolved from apes, but do you know how did interfaces evolve in Java?
Before reading about any Java 9 Features, it would be good if you can think about what are those possible reasons which encouraged Oracle Corporation to include them in their new releases. In fact, not only new releases, but even in older releases, there are many features which intrigues us. For example, What is the use of private constructor in java? or Why Java uses String pool to store String objects?
However, lets start with our today's topic i.e. evolution of interface in java. You can download JDK 1.9 from here based on your OS Platform requirements.
Interface in Java 7 and earlier versions
In Java 7 and earlier versions, following two types of members are allowed in interface.
- Constant variables
- Abstract methods
For Example,
public interface CodePumpkinDemo { String CONSTANT_NAME = "pumpkin"; void abstractMethodOne(); void abstractMethodTwo(); }
We cannot provide Method implementations in interfaces.
If we want to provide the combination of abstract methods and non-abstract methods (Methods with implementation), we should go for Abstract class only. Refer our article interface VS abstract class for more information on this.
Interface in Java 8
Oracle Corporation has introduced some new features to Java Interface in Java SE 8 Release. That is Default Methods and static methods feature.
Why Defaut Method?
Suppose we have one class which is implementaing abstract methods of iCodePumpkin interface as shown in below code.
class CodePumpkin implements iCodePumpkin { @Override public void abstractMethodOne() { System.out.println("First abstract Method implementation"); } @Override public void abstractMethodTwo() { System.out.println("Second abstract Method implementation"); } }
Assume that some other classes have also implementated the same interface. After some years, you get a requirement to add another 2 instance methods. How you will do it?
Modifying this interface will break all classes that extends the interface which means that adding any new method could break millions of lines of code.
Therefore, default methods have introduced as a mechanism to extending interfaces in a backward compatible way. We can add default method to an interface as shown in below code snippet.
interface iCodePumpkin { String CONSTANT_NAME = "pumpkin"; void abstractMethodOne(); void abstractMethodTwo(); default void defaultMethodOne() { //common code System.out.println("First Default Method"); } default void defaultMethodTwo() { //common code System.out.println("Second Default Method"); } }
Default methods can be provided to an interface without affecting implementing classes as it includes an implementation. If each added method in an interface defined with implementation then no implementing class is affected.
An implementing class can override the default implementation provided by the interface.
Why static Method?
Java interface static method is similar to default method except that we can’t override them in the implementation classes. This feature helps us in avoiding undesired results incase of poor implementation in implementation classes.
We can add static methods in our interface iCodePumpkin as shown in below code:
interface iCodePumpkin { // Constants and other abstract and default methods static void staticMethodOne() { //common code System.out.println("First Static Method"); } static void staticMethodTwo() { //common code System.out.println("Second Static Method"); } }
So in Java 8, interface can have below listed members
- Constant variables
- Abstract methods
- Default methods
- Static methods
Sometimes, when we want to introduce several default and static methods, they may share some common code base and then it would be nice if we could use private methods in the interface.
This way, we can reuse our code and also prevent it from being exposed to classes that are using or are implementing the interface.
In Java 8, we can not write private methods in interface. But we can achieve this by implementating inner class in interface as shown in below code snippet.
interface iCodePumpkin { String CONSTANT_NAME = "pumpkin"; void abstractMethodOne(); void abstractMethodTwo(); default void defaultMethodOne() { Hidden.commonCode(); System.out.println("First Default Method"); } default void defaultMethodTwo() { Hidden.commonCode(); System.out.println("Second Default Method"); } class Hidden { private static void commonCode() { // Common Code } } }
The private method of inner class is not visible from outside classes or interfaces but the Hidden class itself can be seen. However, methods and fields in Hidden cannot be seen if they are private.
You can also read our articles on other Java 8 Features like
Interface in Java 9
To provide a resolution to all such headache, Oracle Corporation is going to introduce one new feature to Interfaces as part of Java SE 9 Release: Private methods in Interfaces.
In java 9, additional two members can be included in interface:
- Constant variables
- Abstract methods
- Default methods
- Static methods
- Private methods
- Private Static methods
Example,
Below code shows use of private methods and private static methods.
interface iCodePumpkin { String CONSTANT_NAME = "pumpkin"; void abstractMethodOne(); void abstractMethodTwo(); default void defaultMethodOne() { commonCode(); System.out.println("First Default Method"); } default void defaultMethodTwo() { commonCode(); System.out.println("Second Default Method"); } static void staticMethodOne() { commonStaticCode(); System.out.println("First Static Method"); } static void staticMethodTwo() { commonStaticCode(); System.out.println("Second Static Method"); } private void commonCode() { // Common Code } private static void commonStaticCode() { // Common static method Code } }
Important Points about Private methods in Interface
- We cannot use the combination of private and abstract modifiers, because both have different meanings.
- Private methods must contain body.
- No need to write Duplicate Code i.e. It increases Code Reusability.
- We can expose only our intended methods to clients.
Click Here to read about other Java 9 Features like JShell: The Java Shell And REPL and Convenience Factory Methods for Collections
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: Interface, Java, Java8, Java9
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.