Private Methods In Interfaces In Java 9



Java SE 9 is nearing its release date. At this time we have a fair idea about new features in Java 9. One of the major enhancements is the introduction of Private Methods in Interfaces. Initially, it might be confusing to understand requirement of private methods in an Interface, but let's try to understand it step by step.

Pre Java 8 Era

In pre Java 8 era, Interfaces were only skeletons to define class structure. They were not allowed to have definition for any method.
public interface DBManager{

      String USER_NAME = "xyz";

      String PASSWORD = "*********";


      void save(Employee obj);

      void save(Department obj);

}
This created a situation, where, if we had single behavior for a method, we had to repeat same piece of code in each implementing class. This resulted in lot of redundant code. Another solution was, to use Abstract Class instead of an Interface. This approach had its own obvious problems.

Default Methods in Java 8

Java 8 solved this problem by allowing Interfaces to have default implementation for a method, these were called default methods. A default method had to be marked with default keyword. We can choose not to provide implementation of default methods in implementing Classes.
public interface DBManager{

      String USER_NAME = "xyz";

      String PASSWORD = "*********";


      default void save(Employee obj){

  //1. create connection

  //2. save object.

  //3. close connection

      }
  

      default void save(Department obj){

  //1. create connection

  //2. save object.

  //3. close connection

      }

}
This is a great solution to problems of Java 7 and earlier. But it came with its own drawback. What if we have multiple default methods and there is a code redundancy between these methods? Of course, we can extract the redundant code to a separate default method. But then that method becomes public and can be accessed directly by external classes, instead of being accessed through initial default methods.


Private Methods in Java 9

Private methods in Interfaces in Java 9, come as a required solution to problem of code redundancy in default methods in Java 8.
public interface DBManager{


      String USER_NAME = "xyz";

      String PASSWORD = "*********";


      default void save(Employee obj){

  saveToDb(obj, Employee.class);

      }


      default void save(Department obj){

  saveToDb(obj, Department.class);

      }


     private void saveToDb(Object obj, Class type){

  //1. create connection

  //2. save object with type.

  //3. close connection
     }


}


Now in Java 9, we can extract common code between default methods of an Interface into a private method. The Private Methods of an Interface are not accessible out side an Interface, neither are they inherited by implementing classes.

I hope I have been able to explain the feature and use of Private Methods in Interfaces in Java 9.

Please leave your feedback in comments below.


New Features of Java 9



Java 9 upgrade is almost ready. Early access builds are already out in the wild, with general availability expected in September 2017.

Today we have a very clear picture of what to expect from Java 9 release. If Java 8 was a major release of lambdas, streams and API changes, then Java 9 is all about modules, jshell, and a collection of under the hood and API updates.

Let take a quick look at some of the major features in Java 9.

1. Java 9 Module System

One of the big changes of Java 9 is the Module System. Oracle Corp is going to introduce their Jigsaw Project.

Project Jigsaw’s goal is to make Java modular and break the JRE to inter-operable components.

Modular source code is the first out of 4 steps towards Jigsaw and will not change the actual structure of the JRE and JDK. The purpose of this step is to reorganize the JDK source code into modules, enhance the build system to compile modules, and enforce module boundaries at build time.

Before Java SE 9 versions there is no notion of explicit dependencies between different parts (JAR files) of a system. Every public class can be accessed by any other public class on the classpath, leading to inadvertent usage of classes that weren't meant to be public API. Also, the classpath itself is problematic, how do you know if there are duplicate entries? The module system addresses both issues.

Modular JAR files contain an additional module descriptor. In module descriptor, `requires` statements expresses dependency on other modules. Additionally, `exports` statements control which packages are accessible to other modules, rest all are encapsulated in the module by default.
module MyModule {

  exports com.actigence.finance;

  requires employee;

}

2. Private Methods in Interface

Java 8 allowed Default and Static methods in an Interface. Now Java 9, allows definition of private methods, to avoid redundant code and allow more re-usability.
public interface Employee{

  private Long id(){

    // Method implementation goes here.

  }

  private static void hostedEvents(){

    // Method implementation goes here.

  }

}

3. Improved Process APIs

It has always been a pain to work with OS processes. Java 9 has added some new classes and methods to make it easy to manage and control OS processes.
System.out.println("Current Process Id: = " + ProcessHandle.current().getPid());

4. JShell - Java 9 REPL

REPL stands for Read Evaluate Print Loop. Many languages already feature an interactive Read-Eval-Print-Loop, and Java now joins this club. You can launch JShell from the console and directly start typing and executing Java code. JShell makes a good tool to explore APIs and try out language features.
G:\>jshell

|  Welcome to JShell -- Version 9-ea

|  For an introduction type: /help intro


jshell> int x = 10

x ==> 10


jshell> System.out.println("X value = " + x )

X value = 10

These are few of the Java 9  features that will change the way you code in Java.

Stay tuned for more Java 9 updates.

Please leave your feedback in comments section.


What are variables in SASS?

Sass Variables allow you to define a value once and use it in multiple places. Variables begin with dollar signs and are set like CSS properties. You can change the value of the variable in one place, and all instances where it is used will be updated.

When defining a variable we store in it a value, which often reoccur in the CSS like a palette color, a font stack or the whole specs for a cool box-shadow. Also, mostly the variables are defined at a fixed location, like top of the SASS file or in a dedicated separate file. This makes it easy to locate and change the values, instead of searching through the entire stylesheet.

Below you can see a simple example, where variables are used to define font, color and shadow.

SCSS file:
$title-font: normal 24px/1.5 'Open Sans', sans-serif;
$title-color: #F44336;
$box-shadow-bottom-only: 0 2px 1px 0 rgba(0, 0, 0, 0.2);

h1.title {
  font: $title-font;
  color: $title-color;
}

div.container {
  color: $title-color;
  background: #fff;
  width: 100%;
  box-shadow: $box-shadow-bottom-only;
}
Output CSS file after compilation
h1.title {
  font: normal 24px/1.5 "Open Sans", sans-serif;
  color: #F44336; 
}

div.container {
  color: #F44336;
  background: #fff;
  width: 100%;
  box-shadow: 0 2px 1px 0 rgba(0, 0, 0, 0.2);
}
SASS variables make it extremely easy to organize your stylesheet and make updates.

GrooveUI allows you to create multiple themes, by setting separate values of SASS variables for each theme. New themes can be created using GrooveUI website and values updated in real time.

Creating Dynamic CSS

While creating a SaaS (Software as a Service) software, a major goal is to allow users to have their own branding on the SaaS software. This specifically means, allowing the users to customize colors and images of the software.

Whenever faced with this problem, we start hunting for 'dynamic CSS'. We look for options where we can set values in our CSS stylesheets like colors and image at run-time. However, native CSS technology does not support setting values dynamically. So we come across pre-compilers like SASS and LESS. These pre-compilers support features like variables and rules, but again they do not allow you to change these values at run-time. These pre-compilers are mostly meant to improve organization of your stylesheets and make them easy to maintain.

A software that could allow us to set CSS values from database and reflect those changes in real time (of course without any website downtime or re-compilation) would be an ideal solution.

This is where GrooveUI comes to rescue. GrooveUI is a hosted service that allows you to create multiple themes and control them in real time. So basically you can create multiple themes for your website, specify different CSS values and changes take effect in real time. Also, all this without you having to make any changes to your website, after first setup.

How does GrooveUI work?

The developers of GrooveUI have done the heavy lifting for you, and created a service that allows you to store your CSS variables in database and use them to recreate stylessheets at run time, while taking care of performance issues.

GrooveUI uses industry approved SASS format for stylesheets. All you have to do is upload your SASS files to GrooveUI CDN (only SASS files, other files like HTML and JS are not required) and connect to GrooveUI CDN to access compiled CSS. That's it, now you can use GrooveUI website to create new themes and manage them individually.

You can access different themes by specify theme name in 'theme' parameter on your website URL or adding different domains or sub-domains to you GrooveUI account and configuring default theme for them.

GrooveUI gives you truly dynamic CSS and allows you to convert your SaaS software to while labelled offering in minutes.

What is GrooveUI?

GrooveUI is a web service that allows you to easily convert your website into a white labelled offering. GrooveUI allows you to create and apply new website themes in real time. You can manage stylesheet and images of you website on the fly. GrooveUI helps you to convert your SaaS (Software as a Service) software into a white labelled software in minutes.


GrooveUI is built over industry trusted SASS(Syntactically Awesome StyleSheets) stylesheets. You only have to upload your SASS files to GrooveUI CDN (not other files) and connect your website to retrieve compiled CSS files from GrooveUI CDN. GrooveUI extracts all the SASS variables from your files and gives you a nice UI to update their values. You can create multiple themes and specify different values for each SASS variable based on Theme.

Themes are accessed by specifying 'theme' parameter in your website URL. You can also configure multiple domains and sub-domain and set default theme for each of them. In that case, when you access your website from a configured domain, your website will be rendered using default theme.

GrooveUI helps you create truly dynamic CSS.

SASS with GrooveUI

SASS stands for Syntactically Awesome StyleSheets. Sass is a CSS pre-processor with syntax advancements. Style sheets created in SASS syntax are processed by the SASS compiler, and turned into regular CSS style sheets.
It allows you to easily manage complex CSS files by using variables, nested rules, mixins, inline imports, and more, all with a fully CSS-compatible syntax. Sass helps keep large stylesheets well-organized, and get small stylesheets up and running quickly, particularly with the help of the Compass style library.
One of the most powerful features of SASS is the use of variables. Example, instead of repeating the value of your sites primary color at multiple places, you could define a variable for primary color and then use that throughout your stylesheet. SASS compiler will replace the value of variable with actual value while generating CSS.
One feature which is left wanting, is the ability to dynamically update these variables, say from database values. This is where GrooveUI comes in to picture.
GrooveUI is an online service that allows you to set SASS variables dynamically. It allows you to have multiple active theme and access those themes through different domain or using URL parameters.
You only need to upload your SASS files to GrooveUI CDN and connect your website to GrooveUI CDN for retrieving the compiled CSS. That’s it. Now using GrooveUI website you can create new themes and define individual values for SASS variables. No changes are required on your website. You can control both CSS and Images.