0

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...) ?

James P.
  • 19,313
  • 27
  • 97
  • 155

1 Answers1

8

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • +1, spot on. To add to it, interface methods are implicitly `public` whereas fields are `public` `static` `final` – adarshr Aug 15 '11 at 16:50
  • You're right. I'd completely forgotten about this fact. @adarshr: I have yet to encounter fields in an interface. The following topic states that they act as constants: http://www.coderanch.com/t/408233/java/java/Instance-variables-interface – James P. Aug 15 '11 at 16:58
  • In passing, any opinions on the question about having an ID? – James P. Aug 15 '11 at 17:02
  • 1
    Regardless of the technique used to store your persistent objects, you have to be able to identify them uniquely. So I would say yes. BTW, the id attribute in XML has a special meaning: it uniquely identifies an element in the document. – JB Nizet Aug 15 '11 at 17:10