Implementing "package" interfaces

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

  1. Could someone explain this:

    interface myInterface{
    void foo();
    }


    then...

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


    Thanks



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.711 / Virus Database: 467 - Release Date: 6/25/2004
     
    James D Carroll, Jun 28, 2004
    #1
    1. Advertising

  2. James D Carroll

    Lance Lamboy Guest

    On Sun, 27 Jun 2004 23:21:55 -0400, James D Carroll wrote:

    > Could someone explain this:
    >
    > interface myInterface{
    > void foo();
    > }
    > }
    >
    > then...
    >
    > 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) ?


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


    >
    > Thanks
    >
    >
    >
    > ---
    > Outgoing mail is certified Virus Free. Checked by AVG anti-virus system
    > (http://www.grisoft.com). Version: 6.0.711 / Virus Database: 467 -
    > Release Date: 6/25/2004


    --
    Lance Lamboy

    "I tell them the truth and they think it's hell." ~ Harry S. Truman
     
    Lance Lamboy, Jun 28, 2004
    #2
    1. Advertising

  3. James D Carroll

    xarax Guest

    "Lance Lamboy" <> wrote in message
    news:p...
    > On Sun, 27 Jun 2004 23:21:55 -0400, James D Carroll wrote:
    >
    > > Could someone explain this:
    > >
    > > interface myInterface{
    > > void foo();
    > > }
    > > }
    > >
    > > then...
    > >
    > > 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) ?

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

    /snip/

    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
    #3
  4. xarax wrote:

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


    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
     
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jun 28, 2004
    #4
  5. James D Carroll

    Chris Smith Guest

    Dario wrote:
    > 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.


    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.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    6
    Views:
    515
    Chris Uppal
    Nov 7, 2004
  2. Oliver Wong
    Replies:
    16
    Views:
    623
    Oliver Wong
    Jun 28, 2006
  3. josh
    Replies:
    6
    Views:
    454
    Ed Kirwan
    Dec 19, 2006
  4. Replies:
    13
    Views:
    753
    Dave Rahardja
    Feb 2, 2007
  5. Lawrence D'Oliveiro

    Implementing Unrelated Interfaces

    Lawrence D'Oliveiro, Mar 8, 2011, in forum: Java
    Replies:
    8
    Views:
    272
    markspace
    Mar 10, 2011
Loading...

Share This Page