Package level interfaces

Discussion in 'Java' started by James D Carroll, Jul 5, 2004.

  1. I posted this previously:

    *****
    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
    ****
    I got a one answer that mentioned that when it comes to interfaces they are
    automatically public and abstract. The abstract aspect I accept.

    But isn't there a way to create an interface that can only be implemented by
    classes in the same package? Or more genereally a way to restrict who can
    implement my interfaces? Separately, could an abstract base class be
    declared with "package" level permissions to achieve the same goal (though I
    don't care for that, I's do it if I hafta)

    Thanks,

    PS: Apologies for reposting. I use Outlook Express and it "lost" the
    reponses to my original post.
     
    James D Carroll, Jul 5, 2004
    #1
    1. Advertisements

  2. James D Carroll

    Tony Morris Guest

    I got a one answer that mentioned that when it comes to interfaces they
    are
    Correct.
    Java Language Specification 9.4
    Yes - a different "thing" altogether.

    It appears that you have a design issue.
    I suggest you state your requirements clearly so that you can be assisted
    before naively wandering off in the wrong direction.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Jul 5, 2004
    #2
    1. Advertisements

  3. James D Carroll

    Woebegone Guest

    Your original response pretty much says it all -- interfaces by definition
    are public and abstract. If you accept the abstract part, what is so
    difficult about the public part? If it's not visible, it's not part of an
    interface. As for your second question, an abstract base class could be
    declared, but what is your goal? Who would implement? It would be easier to
    address your questions if you describe more fully what you're trying to
    accomplish.

    I trust that you are satisfied with the earlier explanation of your compiler
    error: the declaration from the interface is implicitly public, but your
    implementing class reduced the access privilege to package.
     
    Woebegone, Jul 5, 2004
    #3
  4. James D Carroll

    Tony Morris Guest

    But isn't there a way to create an interface that can only be
    implemented
    Well, yes, but I assume this isn't what you were asking.

    // package scope interface
    class interface X
    {

    }

    I assume you were asking about the (non-)existance of package scope methods
    of interfaces.
    To want this indicates a design problem, rather than a language deficiency.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Jul 5, 2004
    #4
  5. James D Carroll

    Roedy Green Guest

    Roedy Green, Jul 5, 2004
    #5
    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.