Java 9 Feature Intro #1: Interface private methods.

In my last blog, I listed a number of problems I ran into while trying to write. I then added some fixes I was going to put in place to try and solve them, and hopefully keep my blogging regular. One of these fixes, was that I would try and write blogs that were short, to the point and focussed.

Something I have decided to do in order to try and achieve this, is to blog about the things I have discovered and been playing with, in my daily quest to have fun with the technology I am using currently or might want to use in the future.

Most of my current daily development work is done using Java. Keeping on top of what's new in the Java world is therefore high on my to-do list!

Soon to hit those of us working in this universe is Java 1.9, so I decided a nice topic to cover might be some new features coming in this latest release. I'm going to try and pick some of the smaller, perhaps more unknown features, as there is already lots of discussion on the larger topics. That said, I probably will chime in on a few of those too (I am kind of itching to talk about Jigsaw!)

Anyway, back to the here and now. The feature I am starting with really is a small (but important) one, so what follows really will be short! :)

First, a quick recap.

As we know, Java8 saw some huge changes. Among those changes was the introduction of lambda expressions in Java. Being a recent migrant over from the C# universe, I was especially pleased by this. Oh, how beautiful it is!

Another interesting feature, is the ability to add implementation details to an interface in the form of default methods.

public interface SomeInterface  
{
    void implementMe();

    default void doSomething()
    {
        System.out.println("Hello, I'm doing something");
    }
}

I'm still not 100% sure on my feelings this feature. My current viewpoint is to use only if you are absolutely sure it is necessary. Anyway, that's a discussion for another blog post.

When you have found a fitting place to use the feature, however, there is one thing missing in Java8 - the ability to share functionality between default methods.

The ability to do just that, is what we are getting in Java9 with the addition of private interface methods.

I'm sure the following code snippet isn't wholly necessary, but for completeness (and because I wanted to try out my new PrismJS plugin...)

public interface SomeInterface  
{
    void implementMe();

    default void doSomething()
    {
        // Do something

        doSharedPart();
    }

    default void doSomethingElse()
    {
        // Do something else

        doSharedPart();
    }

    private void doSharedPart()
    {
        // Common functionality
    }
}

A great addition, although it does seem a little strange it wasn't included straight away. Then again, I know as well as anyone not everything can make a release!

Anyway, the important thing is that with Java9 we can all stay DRY and share implementation specifics between default methods. And that makes me a happier developer!