DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Java 9 (Part 3): Super Interfaces, First Look!

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.

Tomer Ben David user avatar by
Tomer Ben David
·
Apr. 25, 17 · Tutorial
Like (14)
Save
Tweet
Share
20.03K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Interface (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Orchestration Pattern: Managing Distributed Transactions
  • Introduction Garbage Collection Java
  • Host Hack Attempt Detection Using ELK
  • Documentation 101: How to Properly Document Your Cloud Infrastructure Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: