Implementing "package" interfaces

Discussion in 'Java' started by James D Carroll, Jun 28, 2004.

  1. Could someone explain this:

    interface myInterface{
    void foo();


    class myClass implements myInterface{
    void foo(){
    'do this

    I want the Interface, its methods, and the implementing class and its
    methods to have "package" level permissions. But I get the complier error:

    attempting to assign weaker access privileges; was public

    Could someone explain this (or provide a link that does) ?

    James D Carroll, Jun 28, 2004
    1. Advertisements

  2. James D Carroll

    Lance Lamboy Guest

    Yes. Interface methods must be public and abstract. Since java knows
    that all interface methods must be public and abstract, it lets you leave
    out the keywords (in an interface).

    A definitive source is the Java Language Specifications (google it).
    Lance Lamboy, Jun 28, 2004
    1. Advertisements

  3. James D Carroll

    xarax Guest


    This is yet another reason why it is a good idea to
    be very explicit when declaring anything in an interface.
    I always use "public abstract void foo();" so that any
    reader, including a newbie, cannot get confused as to
    the intent.
    xarax, Jun 28, 2004
  4. James D Carroll

    Guest Guest

    Java Language Specification discourage your idea:

    For compatibility with older versions of the Java platform,
    it is permitted but discouraged, as a matter of style, to
    redundantly specify the abstract modifier for methods declared
    in interfaces.

    It is permitted, but strongly discouraged as a matter of style,
    to redundantly specify the public modifier for interface methods.

    I follow Java Language Specification suggestions.

    - Dario
    Guest, Jun 28, 2004
  5. James D Carroll

    Chris Smith Guest

    Yes, it does. A large number of people, though, have found that this
    section of the JLS is perhaps worded a bit too strongly. Since it's not
    normative, it's not binding. Personally, I make it a practice to
    declare interface methods as public, though not abstract (the lack of a
    method body is sufficient to convey that, it seems).

    I noticed yesterday that as of the most recent beta of 3.0, Eclipse
    defaults to declaring interface methods public and abstract in the
    'Extract Interface' refactoring; a testament to the number of people who
    find it useful to do so despite the JLS.

    Of course, the choice doesn't really make a lot of difference; it's
    related more to maintaining subconscious connection to code than the
    actual conscious element of understanding it. Competent Java
    programmers know that interface methods are public and abstract anyway,
    but it just often create a "huh" sort of moment to see a method without
    an access specifier there.

    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 28, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.