Java_interface_java_base_class_across_versions

width=”100%”—
layout: blogpost
title: Moving from an interface to a base class across Java versions
image: barak.jpg
author: Barak Merimovich
tags:
– Cloud Driver
– JClouds
– Programming
– Cloud Computing

Java 8 - Java Base Classes + Java Interfaces
Interfaces are one of the core design concepts of Object Oriented Design. They are used for multiple reasons, like enabling multiple implementations of specific interfaces.  In this post, I’ll dive into how these are used in Java 6 and 7, how they apply to Cloudify, and what’s coming with Java 8.
So, just for those who are unfamiliar, in object design an interface is a contract that an object is required to fulfill.  If an object declares that it implements an interface, then it is required to implement all of the methods defined in that interface. This essentially allows developers to separate between the definition (i.e. interface) of an object and its implementation.  In layman’s terms, separating the what from the how.
For instance with Cloudify, at the core of the product we have the cloud driver, which defines how Cloudify allocates resources from the cloud of choice.  Obviously, each cloud has its own API, so there are multiple cloud driver interface implementations.  There is one based on JClouds, the multi-cloud library, one for Microsoft Azure, in addition to several other implementations.  On top of these, users are also able to create their own cloud driver implementations, using Java or Groovy.
The thing with using a Java interface though, is that once you have defined it, and users have started using it, changing it is very difficult.  It’s difficult because if an interface is changed, all of its existing implementations must be changed as well, since this will result in them either breaking during compilation or during runtime.  With Cloudify, where users can create their own cloud driver, and there are multiple versions of Cloudify currently in production, this can get very tricky.


 

Transition your apps on the cloud from Java 7 to Java 8 seamlessly with Cloudify.    Go

 


Now add to this the fact that the cloud world is rapidly changing.  It is a dynamic world with new features coming out all the time, from new cloud APIs, and new storage, network and compute implementations, through new features being added to existing services regularly.  Being able to maintain these services and interfaces without changing them, has become a maintenance challenge in Cloudify.
In order to ‘future-proof’ the cloud driver interface, the Cloudify 2.7 team has decided to shift from using interfaces to using Java abstract classes. By using an abstract base class, Cloudify can add new method declarations into the base interface, as well as add default implementations. For instance throwing a Java standard Unsupported error for newly added methods. This new approach will allow us to add new methods to our “cloud driver interface” without breaking existing cloud drivers.
To ensure backwards compatibility the old cloud driver interface will still be supported, but new cloud driver functionality will not be added to it, so as a result we expect these to be phased out eventually.
Using a base class to define an interface is considered less friendly in Object Oriented Design, as it forces the developer to extend Cloudify’s base class, rather than define their own implementation inheritance themselves.  This is due to the fact that in Java, a class can implement multiple interfaces, but it can only extend one base class.  Therefore, this design is considered intrusive.  However, for the rapidly changing cloud landscape, we have found this to be the best option.
Enter Java 8. While it still has not been released, Java 8 is scheduled to include the option for adding ‘Default Methods’ – default method implementations for an interface.  This enables you to add a method to an interface which includes a default implementation, so that implementing classes will not break if they were compiled with the previous version of the interface.
This basically means that users will be able to publish a cloud driver interface and add methods to it later without breaking compilation or runtime requirements for existing cloud driver implementations.  This issue of interface future compatibility is the reason that Java has chosen to add this feature.
For now, Cloudify supports Java versions 6 & 7. That said, we expect to see this new Java 8 feature of default methods become very useful for the entire Java community.
 

comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top