Java 8 introduced default implementations for interface methods, which had been abstract only up till then. The default implementation meant that a class that implemented the interface did not have to provide an implementation for the method/s.
Problem: But, another problem still remained. If m interface methods with default implementation have n common code logic blocks, the common code logic has to be included in each of the m methods. As a result, n common code logic blocks referred in n methods with default implementation resulted in mxn code logic blocks.
Solution: Java 9 introduced private interface methods. The private interface methods could be used to encapsulate common code logic that was used in the interface methods that provided a default implementation. The common code logic could be invoked in a default implementation method in an interface. Being a private method, the common code logic cannot be overridden in a class that implements the interface. The mxn code logic blocks are reduced to n code logic blocks. As an example, two interface methods, method1 and method2, that invoked common code logic may make use of a private method, method3, to encapsulate the common code logic.
In two articles, we shall discuss the private interface methods introduced in Java 9, with an example in Eclipse IDE.
This article has the following sections:
- Setting the Environment
- Creating a Java Project
- Setting Java Version and Preferences
- Creating a Java Interface
- Conclusion
Setting the Environment
Download and install an Eclipse edition that supports Java 9. Eclipse IDE for Java EE Developers Photon M6 package (Windows 64-bit) is used in this article. Download and install Java 9.
Creating a Java Project
First, we need to create a Java project. Select File>New>Other in Eclipse IDE, as shown in Figure 1.
Figure 1: File>New>Other
In the New window, select Java>Java Project and click Next, as shown in Figure 2.
Figure 2: New>Java>Java Project
The Create a Java Project window gets launched, as shown in Figure 3. Specify a Project name (MagazineSubscription), and select the Use default location checkbox. The JRE should be set to JavaSE-9. For Project layout, select Create separate folders for sources and class files. Click Next.
Figure 3: New Java Project>Create a Java Project
In Java Settings, select Allow output folders for source folders checkbox, as shown in Figure 4. With Default output folder set to MagazineSubscription/bin, click Finish.
Figure 4: Java Settings
The Open Associated Perspective dialog indicates that the Java perspective is associated with the kind of project selected. Click Open Perspective, as shown in Figure 5.
Figure 5: Open Java Perspective
The Java perspective may be opened at a later stage by selecting Window>Perspective>Open Perspective>Java (see Figure 6).
Figure 6: Window>Perspective>Open Perspective>Java
The Java perspective icon gets added, as shown in Figure 7.
Figure 7: Java Perspective Icon
We also need to open some Views by selecting Window>Show View, as shown in Figure 8.
Figure 8: Window>Show View
Open the Package Explorer, Project Explorer, Console, and Outline Views, as shown in Figure 9. The MagazineSubscription project is also shown in in the Project Explorer View.
Figure 9: Package Explorer, Project Explorer, Console, and Outline Views
Setting Java Version and Preferences
To set or verify the Java version and preferences, select Window>Preferences, as shown in Figure 10.
Figure 10: Window>Preferences
The Java preferences settings are shown in Figure 11.
Figure 11: Java Preferences
The Java Compiler settings should be set to JDK Compiler compliance level 9, as shown in Figure 12.
Figure 12: Compiler Compliance Level set to 9
Creating a Java Interface
In this section, we shall create a Java interface making use of private method/s. A new Java interface may be created by selecting one of the following options:
- Right-click the project in Project Explorer and select New>Interface, as shown in Figure 13.
- Select File>New>Interface.
- Select Java>Interface in New window, as listed in Figure 2, and click Next.
- Select Interface in the New window and click Next.
Figure 13: Project>New>Interface
Selecting any of the options starts the New Java Interface window, as shown in Figure 14. The Source folder is set to MagazineSubscription>src by default. Specify Package name (magazinesubscription) and an interface Name (MagazineSubscription), and click Finish.
Figure 14: New Java Interface
A new Java interface, MagazineSubscription, gets created, as shown in Figure 15.
Figure 15: Java Interface MagazineSubscription
Next, add methods to the interface. We shall use an example for a magazine subscription in which the subscription code could change and the other magazine details including magazine name, magazine edition (print or digital), and magazine subscription are based on the subscription code. If we were to directly code the subscription code in the other magazine details, we would need to modify each reference to the subscription code if the subscription code were to change; this would involve several code updates. This is the kind of application the private method in the interface has been provisioned for. We shall encapsulate the common code logic in a private method, getMagazine(int subscriptioncode), which returns the magazine based on the subscription code argument supplied. Once the magazine is known, the default settings for other magazine details may be coded in separate methods. The private method getMagazine(int subscriptioncode) is listed.
private String getMagazine(int subscriptioncode) { if (subscriptioncode == 1) return "Oracle Magazine"; else if (subscriptioncode == 2) return "Java Magazine"; else return "Not Valid Subscription Code"; }
Next, add methods that include default implementation for getting the magazine name, magazine edition, and magazine frequency. The MagazineSubcription interface is listed:
package magazinesubscription; public interface MagazineSubscription { private String getMagazine(int subscriptioncode) { if (subscriptioncode == 1) return "Oracle Magazine"; else if (subscriptioncode == 2) return "Java Magazine"; else return "Not Valid Subscription Code"; } default String getMagazineFrequency(int subscriptioncode) { if (getMagazine(subscriptioncode).equals("Oracle Magazine")) return "Weekly"; else if (getMagazine(subscriptioncode).equals("Java Magazine")) return "Monthly"; else return "Not Valid Subscription Code"; } default String getMagazineEdition(int subscriptioncode) { if (getMagazine(subscriptioncode).equals("Oracle Magazine")) return "Print only"; else if (getMagazine(subscriptioncode).equals("Java Magazine")) return "Digital Only"; else return "Not Valid Subscription Code"; } default String getMagazineName(int subscriptioncode) { if (getMagazine(subscriptioncode).equals("Oracle Magazine")) return "Oracle Magazine"; else if (getMagazine(subscriptioncode).equals("Java Magazine")) return "Java Magazine"; else return "Not Valid Subscription Code"; } }
Copy the code to the MagazineSubscripton interface in Eclipse. To format the code, right-click in the interface and select Source>Format (see Figure 16).
Figure 16: Source>Format
The formatted MagazineSubscription interface is shown in Figure 17.
Figure 17: Formatted MagazineSubscription interface
Conclusion
In this article, we introduced private interface methods, a new feature in Java 9. We discussed its need and created a Java project to use private interface methods. First, we created an interface that includes a private interface method. In a subsequent article, we shall implement the interface in a class to demonstrate the use of private interface methods.