James P.
James P.

Reputation: 19607

Why use default access for methods in a generic DAO interface?

I found this code for a generic DAO interface while browsing around:

public interface GenericDAO<T, ID extends Serializable> {

    Class<T> getEntityClass();

    T findById(final ID id);

    List<T> findAll();
    List<T> findByExample(final T exampleInstance);

    List<T> findByNamedQuery(
        final String queryName,
        Object... params
    );

    List<T> findByNamedQueryAndNamedParams(
        final String queryName,
        final Map<String, ?extends Object> params
    );

    int countAll();

    int countByExample(final T exampleInstance);

    T save(final T entity);

    boolean delete(final T entity);
}

Is there any reason in particular for leaving methods with the default access modifier (class/package: yes, subclass/world: no)?

P.S: An added question. Are IDs usually found in implementations which don't depend on a RDBMS (XML, flat file...) ?

Upvotes: 0

Views: 757

Answers (1)

JB Nizet
JB Nizet

Reputation: 691655

Methods of an interface are implicitely public. Using the public modifier is thus redundant and unnecessary.

Checkstyle even has a rule to check that public is not used in interface methods.

Upvotes: 8

Related Questions