Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java 9 (Part 3): Super Interfaces, First Look!

DZone's Guide to

Java 9 (Part 3): Super Interfaces, First Look!

It's a bird! It's a plane! No, it's Super Interface! Java 9 is bringing private methods to your interfaces, letting you reuse functions.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In Scala, traits can have an implementation, so why not have implementations in Java? Well, that was introduced in Java 8, so the next natural step in Java 9 is to have private logic in interfaces. In Part 2 of this series, we introduced the Java 9 REPL. In this part, we are going to examine the new functionality introduced to interfaces.

Image title

Java 9 is introducing a lot of new concepts, such as REPL, factory methods for common data structures, and more. Let’s see what Java 9 has to say about private methods in interfaces.

Although we refer to the previous tutorial, we really like to start from zero — and what zero means for us is going to the website where you can download Java 9 and installing it. We'll do that in step one, as we don't like incomplete tutorials.

Our plan:

  1. Download Java 9 from scratch and install

  2. Create a Java 8 style interface with the default implementation

  3. Implement the interface and call it

  4. Enrich the interface with a private method. Run the class and see that it reuses the private interface method.

Step 1: Download Java 9

Although we already covered downloading Java 9, we always want to start from the ground floor. We don’t want to force you to go to another tutorial in order to get any step, so here is how to download it into your macOS.

Go to: https://jdk9.java.net/download/ and click on the JDK relevant to your favorite and used OS.

And after downloading it, just click it to install (if you are on macOS) and verify you have it installed:

tomerb@tomerb-mac.local:~$ java --version
java 9-ea
Java(TM) SE Runtime Environment (build 9-ea+164)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+164, mixed mode)


And it’s up and running.

Step 2: Code an Interface

We are going to use JShell to create an interface.

jshell> tomerb@tomerb-mac.local:~/tmp/java9-modules$ jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> interface MyInterface {
   ...> default int doubleThis(int i) {
   ...> return i * 2;
   ...> }
   ...> }
|  created interface MyInterface


So now we have an interface called MyInterface. In the next step, we are going to implement it.

Step 3: Implement and Calling the Interface

So let’s implement the interface and then call it. We are not going to define any methods in the class, so the default implementation would come from the, you guessed it, the default implementation in the interface.

jshell> class MyClass implements MyInterface {
   ...> }
|  created class MyClass

jshell> new MyClass().doubleThis(2); // MyInterface just doubledThis!
$3 ==> 4


Step 4: Adding Private Methods

So we are going to define three methods:

  1. helpDouble: Private. This will... help doubling.
  2. doubleThis: Public. Uses the private helpDouble.
  3. doubleDoubleThis: Public. Uses the private helpDouble.

So helpDouble, the private function, is going to get reused, which is basically why we ned private methods.

jshell> interface MyInterface {
   ...> private int helpDouble(int i) {
   ...> return i * 2;
   ...> }
   ...> default int doubleThis(int i) {
   ...> return helpDouble(i);
   ...> }
   ...> default int doubleDoubleThis(int i) {
   ...> return helpDouble(helpDouble(i));
   ...> }
   ...> }
|  replaced interface MyInterface


Step 5: Run the Code

Next, let’s reinstantiate our class and call doubleThis and doubleDoubleThis, which are reusing helpDouble:

jshell> new MyClass().doubleThis(2);
$5 ==> 4

jshell> new MyClass().doubleDoubleThis(2);
$6 ==> 8

jshell> new MyClass().helpDouble(2);
|  Error:
|  cannot find symbol
|    symbol:   method helpDouble(int)
|  new MyClass().helpDouble(2);
|  ^----------------------^


As you could see, the REPL nicely replaced our definition of the interface and has doubled and doubleddDoubled our 2 by reusing the private method defined in the interface MyInterface.helpDouble.

Summary

After using REPL in the previous tutorial, we went on to investigate Java 9 by checking out its private interface methods. We started again from ground zero. We downloaded Java 9 from scratch, then ran JShell, the new REPL provided by Java 9. We then defined and ran our first interface with a default implementation, then created a class to implement that interface (but that’s so Java 8). So we went on and defined a private method with an implementation in the interface that helped the public interface implementations! Stay tuned for the next parts!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,java 9 ,interfaces ,tutorial ,private methods ,jshell

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}