Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...