Java 9 supports Java 9 private methods in Interface to enable code sharing between non-abstract methods.

Consider java 8 example where we want to print market two most popular search engines Solr and elastic search features.

Example 1 : Java 8 Support Default Method in interface

public interface ISearchEngine {
    
    default void printSolrFeatures(){
        System.out.println("Http Rest Api");
        System.out.println("ID field for updates and deduplication");
        System.out.println("DocValues");
        
        System.out.println("Query-time synonyms");
        System.out.println("Master-slave replication");
        System.out.println("Data import:DataImportHandler - JDBC, CSV, XML, Tika, URL, Flat File");
    }

    default void printElasticSearchFeatures(){

        System.out.println("Http Rest Api");
        System.out.println("ID field for updates and deduplication");
        System.out.println("DocValues");
        
        System.out.println("Multiple document types per schema");
        System.out.println("Data import:Rivers modules - ActiveMQ, Amazon SQS, CouchDB, Dropbox, " +
                "DynamoDB, FileSystem, Git, GitHub, Hazelcast, JDBC");
    }
}

As we see in above example most of the features are same in both the search engine.It’s redundancy of code.

To avoid such problems java 9 interface provide handly features called private methods.

Example 2: Java 9 private methods in interface

public interface ISearchEngine {

    private void printCommonFeatures(){                // java 9 allowed to create private methods, It can not be accessible out site of interface
        System.out.println("Http Rest Api");
        System.out.println("ID field for updates and deduplication");
        System.out.println("DocValues");
    }

    default void printSolrFeatures(){
        printCommonFeatures();
        System.out.println("Query-time synonyms");
        System.out.println("Master-slave replication");
        System.out.println("Data import:DataImportHandler - JDBC, CSV, XML, Tika, URL, Flat File");
    }

    default void printElasticSearchFeatures(){
        printCommonFeatures();
        System.out.println("Multiple document types per schema");
        System.out.println("Data import:Rivers modules - ActiveMQ, Amazon SQS, CouchDB, Dropbox, " +
                "DynamoDB, FileSystem, Git, GitHub, Hazelcast, JDBC");
    }
}

Great! we can write private method using private access modifiers.

Output:

************ Solr Features *************
Http Rest Api
ID field for updates and deduplication
DocValues
Query-time synonyms
Master-slave replication
Data import:DataImportHandler - JDBC, CSV, XML, Tika, URL, Flat File
************ Elastic Search Features *************
Http Rest Api
ID field for updates and deduplication
DocValues
Multiple document types per schema
Data import:Rivers modules - ActiveMQ, Amazon SQS, CouchDB, Dropbox, DynamoDB, FileSystem, Git, GitHub, Hazelcast, JDBC

 

java 9 and later release interface can have following:

  1. Constant variables
  2. Abstract methods
  3. Default methods
  4. Static methods
  5. Private methods
  6. Private static methods
Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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