Rezha Julio

Hi!
My name is Rezha Julio
I am a chemist graduate from Bandung Institute of Technology. Currently working as Data Engineer at Traveloka.
You can reach me by email:

contact@rezhajulio.id

, @ Q Q

Powered by Hugo

filter by tags

Java 101Using an interface as a parameter

time to read 1 min | 151 words

You can define methods that take an interface as a parameter. Your interface defines a contract and your methods will accept as parameter any objects whose class implements that interface. This is in fact one of the most common and useful ways to use an interface.

interface Test {
  public void test(); //define the interface
}

class Tester {
  public void runTest(Test t) {
    t.test(); 
  } // method with interface as param
}

MyTest class will implement this interface:

class MyTest implements Test {
  public void test() { // running code }
}

Now the runTest method will take as a parameter any object that implements the Test Interface:

Tester tester = new Tester(); Test test1 = new MyTest(); tester.runTest(test1);

The collection framework from the standard Java API frequently uses this procedure. For example, Collections.sort() can sort any class that implements the List interface and whose contents implement the Comparable interface.

Java 101Future of Interface in Java 9

time to read 4 min | 681 words

Interface is a very popular way to expose our APIs. Until Java 7 it was getting used as a contract (abstract method) where a child class was obliged to implement the contract; but after Java 8, Interface got more power — to where we can have static and default methods.

In Java 9, Interface is getting more power, to the point where we can define private methods as well. Let us understand why we need private methods in Interfaces.

Suppose we are defining a ReportGenerator Interface in Java8 as below:

package id.rezhajulio.test.interfacejava8;

public interface ReportGeneratorJava8 {

    /**

     * Need to get implemented as per ReportGenerator class

     * @param reportData

     * @param schema

     */

    void generateReport(String reportData, String schema);

    /**

     * Get the ready data

     * @param reportSource

     * @return

     * @throws Exception

     */

    default String getReportData(String reportSource) throws Exception {

        String reportData = null;

        if (null == reportSource) {

            throw new Exception("reportSource can't be null....");

        }

        if (reportSource.equalsIgnoreCase("DB")) {

            System.out.println("Reading the data from DB ....");

            //logic to get the data from DB

            reportData = "data from DB";

        } else if (reportSource.equalsIgnoreCase("File")) {

            System.out.println("Reading the data from FileSystem ....");

            //logic to get the data from File

            reportData = "data from File";

        } else if (reportSource.equalsIgnoreCase("Cache")) {

            System.out.println("Reading the data from Cache ....");

            //logic to get the data from Cache

            reportData = "data from Cache";

        }

        System.out.println("Formatting the data to create a common standard");

        /** Format the data and then return **/

        //logic to format the data

        return reportData;

    }

}

And the implementation class could be HtmlReportGeneratorJava8:

package id.rezhajulio.test.interfacejava8;

public class HtmlReportGeneratorJava8 implements ReportGeneratorJava8 {

    @Override

    public void generateReport(String reportData, String schema) {

        //HTML Specific Implementation according to given schema

    }

}

Method ‘getReportData’ looks pretty messy, as there is a lot of logic that could be kept in a separate method and that can be called in ‘getReportData’. To achieve that, we need a private method, as we don’t want to expose these methods to outside the world.

Another thing, ReportGeneratorJava8 interface is formatting the data after getting it from the source. So we can have a common method named ‘formatData’ defined as private in the interface. So the interface could be rewritten as below:

package id.rezhajulio.test.interfacejava9;

public interface ReportGeneratorJava9 {

    /**

     * Need to get implemented as per ReportGenerator class

     * @param reportData

     * @param schema

     */

    void generateReport(String reportData, String schema);

    /**

     * Reading the report data from DB

     * @return

     */

    private String getReportDataFromDB() {

        System.out.println("Reading the data from DB ....");

        //logic to get the data from DB

        String reportData = "data from DB";

        return formatData(reportData);

    }

    /**

     * Reading the report data from FileSystem

     * @return

     */

    private String getReportDataFromFile() {

        System.out.println("Reading the data from FileSystem ....");

        //logic to get the data from File

        String reportData = "data from File";

        return formatData(reportData);

    }

    /**

     * Reading the report data from cache

     * @return

     */

    private String getReportDataFromCache() {

        System.out.println("Reading the data from Cache ....");

        //logic to get the data from Cache

        String reportData = "data from Cache";

        return formatData(reportData);

    }

    /**

     * Formatting the data to create a common standardized data,

     * as it's coming from different systems

     * @param reportData

     * @return

     */

    private String formatData(String reportData) {

        System.out.println("Formatting the data to create a common standard");

        /** Format the data and then return **/

        //logic to format the data

        return reportData;

    }

    /**

     * Get the ready data

     * @param reportSource

     * @return

     * @throws Exception

     */

    default String getReportData(String reportSource) throws Exception {

        String reportData = null;

        if (null == reportSource) {

            throw new Exception("reportSource can't be null....");

        }

        if (reportSource.equalsIgnoreCase("DB")) {

            reportData = getReportDataFromDB();

        } else if (reportSource.equalsIgnoreCase("File")) {

            reportData = getReportDataFromFile();

        } else if (reportSource.equalsIgnoreCase("Cache")) {

            reportData = getReportDataFromCache();

        }

        return reportData;

    }

}

Now the above implementation looks pretty clean, and we’ve seen the need of private methods in Interface. Summary of Interface Enhancements

  • Constants (until Java 1.7)

  • Method signatures (until Java 1.7)

  • Nested types (until Java 1.7)

  • Default methods (since 1.8)

  • Static methods (since 1.8)

  • Private methods (since 1.9)

  • Private static methods (since 1.9)

Enjoy the power of Java 9’s Interface.

Java 101More about Interface in Java 8

time to read 4 min | 695 words

Interface was meant to define a contract before Java 8, where we were able to define the methods a class needed to implement if binding himself with the interface. Interface was only involved with abstract methods and constants.

But in Java 8, Interface has become much more, and now it can have methods defined using static or default. Remember that default methods can be overridden, while static methods cannot.

Method Definitions

Interface was meant for good designers because when we create an Interface, we should know the possible contract that every class should implement. Once your contract definition is done and classes have implemented the contract, it’s difficult to change the definition of an Interface, as it will break the implemented classes.

A good designer always creates an interface and provides a base class, which provides the default definition of the Interface methods, and classes should extend the base class in place of implementing the interface directly. In this way, any future change in Interface could be taken care of by the base class. Other implementations of sub-classes would be fine.

In Java 8, they tried to fix this issue by providing method definitions — using static or default. We can add the definitions using static or default in Interface without breaking the existing classes, making it easier to design interfaces in Java 8.

Do We Still Need Abstract Classes?

After the Java 8 interface enhancements, the new version of Interface looks like a great replacement for the Abstract class, right? No, not at all. There are still the differences between these two. Do you remember ‘access specifiers’ (public, protected, etc.)?

So the Abstract class can have any of these access specifiers for their methods and variables while an Interface is always public and variables in an Interface are always constants. So we need to be very wise when choosing between the Interface and Abstract classes, and I believe that some intelligence is still required.

Examples of Interface Enhancements

We can define an Interface as having static and default methods as below:

Java8Interface.java:

package id.rezhajulio.interface.enhancement;

public interface Java8Interface {

    //defaults method - by default it's public

    default void hi() {

        System.out.println("In Java8Interface: new feature of Java8 is saying Hi....");

    }

    //static method - by default it's public

    static void hello() {

        System.out.println("In Java8Interface: new feature of Java8 is saying Hello....");

    }

}

The concrete class that is implementing the above Interface can be used as below.

ConcreteClass.java:

package package id.rezhajulio.interface.enhancement;

public class ConcreteClass implements Java8Interface {

    public static void main(String[] args) {

        ConcreteClass c = new ConcreteClass();

        c.hi(); // would be accessible using class instance

        //Not possible to call using class instance

        Java8Interface.hello();

    }

}

Which will lead to this output:

In Java8Interface: new feature of Java8 is saying Hi....

In Java8Interface: new feature of Java8 is saying Hello....

But suppose you have two interfaces with the same method signature as ‘default’. Then, your class should define the implementation for that method. Otherwise, you’ll see a compile time error.

Let’s see how that plays out below. The following Interface has the same methods as our Java8Interface.

Java8Interface_1.java:

package id.rezhajulio.interface.enhancement;

public interface Java8Interface_1 {

    default void hi() {

        System.out.println("In Java8Interface_1: new feature of Java8 is saying Hi....");

    }

    static void hello() {

        System.out.println("In Java8Interface_1: new feature of Java8 is saying Hello....");

    }

}

Meanwhile, our concrete class handles the implementation for our default method as follows:

ConcreteClass.java:

package id.rezhajulio.interface.enhancement;

public class ConcreteClass implements Java8Interface, Java8Interface_1 {

    public static void main(String[] args) {

        ConcreteClass c = new ConcreteClass();

        c.hi();

        Java8Interface.hello();

    }

    //We need to override hi method otherwise compilation error

    @Override

    public void hi() {

        Java8Interface.super.hi();

    }

}

And our output will be as follows:

In Java8Interface: new feature of Java8 is saying Hi....

In Java8Interface: new feature of Java8 is saying Hello....

Next, say a List API has the same default implementation as below:

default void sort(Comparator c))

That helps call the sort methods on our List, and no more Collections API is needed.

And let’s see the snippet for calling the sort method using a List API:

List list = new ArrayList<>(); list.add("v");

list.add("a");

list.add("z"); list.add("d");

list.sort((val1, val2) -> val1.compareTo(val2));

Enjoy the power of Interface!

Java 101Updating interfaces by using default methods

time to read 1 min | 127 words

Take the following interface:

public interface Cooking {
  public void fry();
  public void boil();
  public void chop();
}

To add new functionality, simply adding a new method to Cooking called microwave() will cause problems. Any class that previously implemented Cooking will now have to be updated in order to function again.

To avoid this, give microwave() a default implementation:

public interface Cooking {
  public void fry();
  public void boil();
  public void chop();
  default void microwave() {
    //some code implementing microwave
  }
}

As microwave() already has a default implementation defined in the Cooking interface definition, classes that implement it now don’t need to implement microwave() in order to work.

This allows us to add functionality without breaking old code.

Note: This has been possible since Java 8.

RECENT SERIES

  1. java 101 (13):
    Apr 29, 2017 - Translating Scanner tokens into primitive types
  2. python data structure (5):
    May 03, 2017 - Enhance your tuples
  3. python function (2):
    Apr 16, 2017 - Lambda Functions in Python
  4. python generator (4):
    Apr 26, 2017 - Next, Function or Method ?

Friends of Rezha