Using Private Interface Methods in Java 9, Part 2

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.

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.

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.

Figure 2: New Java Class

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

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.

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.

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.

Figure 7: Project>Properties

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

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.

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

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.

IT Solutions Builder
TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD

Which topic are you interested in?

Mobile

Security

Networks/IoT

Cloud

Data Storage

Applications

Development

IT Management

Other

What is your company size?

What is your job title?

What is your job function?

Searching our resource database to find your matches...

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date