Search code examples
javainterfacejava-9private-methods

Private interface methods are supported


Private interface methods are supported by Java 9.

This support allows non-abstract methods of an interface to share code between them. Private methods can be static or instance.

Can private methods of an interface be abstract or default?

May I ask for an example where "private static interface methods" are useful in terms of code?


Solution

  • No, the private methods in the interfaces are supposedly designed for clubbing in a piece of code that is internal to the interface implementation. Since these pertain to the implementation(consist of a body) and not the declaration it can neither be default and nor abstract when defined.

    A private method is a static method or a non-default instance method that's declared with the private keyword. You cannot declare a default method to also be private because default methods are intended to be callable from the classes that implement their declaring interfaces.


    The private static methods are useful in abstracting a common piece of code from static methods of an interface while defining its implementation.

    Example of a private static method in an interface could be as follows. Consider an object, Question.java on StackOverflow defined as:

    class Question {
        int votes;
        long created;
    }
    

    and an interface that proposes the sort by functionality as seen in the listed questions on StackOverflowTag :

    public interface StackOverflowTag {
    
        static List<Question> sortByNewest(List<Question> questions) {
            return sortBy("NEWEST", questions);
        }
    
        static List<Question> sortByVotes(List<Question> questions) {
            return sortBy("VOTE", questions);
        }
    
        //... other sortBy methods
    
        private static List<Question> sortBy(String sortByType, List<Question> questions) {
            if (sortByType.equals("VOTE")) {
                // sort by votes
            }
            if (sortByType.equals("NEWEST")) {
                // sort using the created timestamp
            }
            return questions;
        }
    }
    

    Here the private static method sortBy of the interface internally implements the sorting based on the sortOrderType sharing the implementation with two public static methods of the interface which can be further consumed by a StackOverflowTagConsumer can simply access these interface static methods as :

    public class StackOverFlowTagConsumer {
    
        public static void main(String[] args) {
            List<Question> currentQuestions = new ArrayList<>();
    
            // if some action to sort by votes
            displaySortedByVotes(currentQuestions);
    
            // if another action to sort by newest
            displaySortedByNewest(currentQuestions);
        }
    
        private static void displaySortedByVotes(List<Question> currentQuestions) {
            System.out.println(StackOverflowTag.sortByVotes(currentQuestions));
        }
    
        private static void displaySortedByNewest(List<Question> currentQuestions) {
            System.out.println(StackOverflowTag.sortByNewest(currentQuestions));
        }
    }