JavaData & JavaUsing Private Interface Methods in Java 9, Part 2

Using Private Interface Methods in Java 9, Part 2

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

In the first article, “Using Private Interface Methods in Java 9, Part 1,” we introduced private interface methods in Java 9. We started by creating a Java project and a Java interface that includes a private interface method. In this continuation article, we shall create and run a Java application that makes use of private interface method/s.

This article has the following sections:

Creating a Java Application

In this section, we shall create a Java class that implements the interface. To run the Java class as an application, we shall add a public static void main(String[] args) method. To create a Java class, several options are available:

  • Right-click the project in Project Explorer, and select New>Class.
  • Select File>New>Class.
  • Select Java>Class in the New window, as shown in Figure 1, and click Next.
  • Select Class in New window, and click Next.

New>Java>Class
Figure 1: New>Java>Class

In the New Java Class wizard, specify a class Name (MagazineSubscriptionImpl), as shown in Figure 2. The Source folder and package are the same as for the interface. Click Add in the Interfaces field, as shown in Figure 2, to add an interface.

New Java Class
Figure 2: New Java Class

In the Implemented Interfaces Selection, select the magazinesubscription.MagazineSubscription interface, as shown in Figure 3, and click OK.

Implemented Interface Selection
Figure 3: Implemented Interface Selection

The interface gets added in Interfaces, as shown in Figure 4. Select the public static void main (String[] args) method in which method stubs would you like to create? Click Finish.

Creating a new Java Class
Figure 4: Creating a new Java Class

The new Java class gets created, as shown in Figure 5.

Java Class MagazineSubscriptionImpl that implements interface MagazineSubscription
Figure 5: Java Class MagazineSubscriptionImpl that implements interface MagazineSubscription

Next, we shall interface implementation code to the class. First, add a variable declaration and class constructor.

public static int subscriptioncode;
   public MagazineSubscriptionImpl(int subscriptioncode) {
      this.subscriptioncode = subscriptioncode;

}

Because all public methods in the interface provide a default implementation, we don’t need to implement any of the interface methods in the class. But, we shall override the getMagazineEdition(int subscriptioncode) method as follows.

public String getMagazineEdition(int subscriptioncode) {

   if (subscriptioncode == 1)
      return "Print and Digital";
   else if (subscriptioncode == 2)
      return "Digital Only";
   else
      return "Not Valid Subscription Code";

}

In the main method, create an instance of the class.

MagazineSubscriptionImpl magazineSubscription = new
   MagazineSubscriptionImpl(Integer.valueOf(args[0]));

The constructor arg subscriptioncode is to be configured separately, as discussed in next section.

Invoke the class methods to output the magazine name, magazine edition, and magazine frequency. The MagazineSubscriptionImpl class is as follows.

package magazinesubscription;

public class MagazineSubscriptionImpl implements
      MagazineSubscription {
   public static int subscriptioncode;
   public MagazineSubscriptionImpl(int subscriptioncode) {
      this.subscriptioncode = subscriptioncode;
   }

   public String getMagazineEdition(int subscriptioncode) {

      if (subscriptioncode == 1)
         return "Print and Digital";
      else if (subscriptioncode == 2)
         return "Digital Only";
      else
         return "Not Valid Subscription Code";

   }

   public static void main(String[] args) {
      MagazineSubscriptionImpl magazineSubscription = new
         MagazineSubscriptionImpl(Integer.valueOf(args[0]));
      System.out.println("Magazine Name: " + magazineSubscription
         .getMagazineName(subscriptioncode) + "n");
      System.out.println("Magazine Edition: " + magazine
         Subscription.getMagazineEdition(subscriptioncode) + "n");
      System.out.println("Magazine Frequency: " +
         magazineSubscription.getMagazineFrequency
         (subscriptioncode) + "n");

   }
}

Copy the class to the Eclipse IDE (see Figure 6).

Java Application MagazineSubscriptionImpl
Figure 6: Java Application MagazineSubscriptionImpl

Setting Program Arguments

We need to supply one program argument for the subscription code. In this section, we shall configure the subscription code program arg. Right-click the project in Project Explorer, and select Properties, as shown in Figure 7.

Project>Properties
Figure 7: Project>Properties

In the Properties window, select Run/Debug Settings and click New…, as shown in Figure 8.

Run/Debug…>New
Figure 8: Run/Debug…>New

In Select Configuration Type, select Java Application, as shown in Figure 9.

Selecting Configuration Type as Java Application
Figure 9: Selecting Configuration Type as Java Application

The Edit Configuration window gets launched, as shown in Figure 10. Browse and select Project as MagazineSubscription. Next. Click Search… for the Main class.

Main Class>Search…
Figure 10: Main Class>Search…

In Select Main Type, select the MagazineSubscriptionImpl class and click OK (see Figure 11).

MagazineSubscriptionImpl Class
Figure 11: MagazineSubscriptionImpl Class

The Main class gets added. Optionally, modify the default configuration name. To specify a program argument, click the Arguments tab, as shown in Figure 12.

Main Class selected and selecting Arguments tab
Figure 12: Main Class selected and selecting Arguments tab

In Arguments, specify an integer value (1) for the subscription code in the Program arguments field, as shown in Figure 13. Click OK.

Specifying Program Argument for Subscription Code
Figure 13: Specifying Program Argument for Subscription Code

The run/debug configuration gets created, as shown in Figure 14. Click Apply and Close.

Run/Debug Configuration added
Figure 14: Run/Debug Configuration added

Running the Java Application

In this section, we shall run the Java application with the configured program arg for the subscription code. Right-click MagazineSubscriptionImpl.java and select Run As>Java Application, as shown in Figure 15.

MagazineSubscriptionIm>Run As>Java Application
Figure 15: MagazineSubscriptionIm>Run As>Java Application

In Save and Launch, select resources to save and click OK (see Figure 16).

Save and launch
Figure 16: Save and launch

The application runs and outputs the magazine name, magazine edition, and magazine frequency, as shown in Figure 17.

Console Output for Java Application
Figure 17: Console Output for Java Application

Conclusion

In two articles, we introduced a new feature in Java 9, private interface methods. We also discussed how private methods could be used to encapsulate common code logic that is invoked in the default implementation of other interface methods. We started by creating a Java project and added a Java interface to describe a magazine subscription. The interface defines one private method for subscription code and three public methods with default implementation. We invoked the private method in the default implementation of each of the public methods’ default implementations. Next, we created a Java application that implements the interface to override a public method; private method/s cannot be overridden. Before running the application, we set the program arg for variable subscription code that is used to infer the magazine details. In the Java application main method, we output magazine details based on program args set previously. We ran the application to output magazine details demonstrating the use of private method/s in an interface as used with a default implementation of public interface methods.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories