Standard visibility for abstract methods

java

Solution

Depends on your use case. If the abstract method only implements some piece of a greater functionality that is available from a public method in your abstract class, then it should probably be protected. If it is a standalone method that can/should be called from another class, make it public.

Examples:

public abstract class Foo implements Closeable {
    public final void close() {
        // do whatever
        doClose();
    }

    protected abstract void doClose();
}

public abstract class Bar {
    public void read(byte[] b) {
        for(int x = 0; x < b.length; x++) {
            b[x] = read();
        }
    }

    public abstract int read();
}

Problem

This may seem like a silly question, but I'd like to know the "best practices" when creating abstract methods. Should their visibility be public or protected? Even though a child class implementing the abstract method will be public, is it still advisable to maintain the abstract method protected?

Original source