Implementing "package" interfaces

J

James D Carroll

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
 
L

Lance Lamboy

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

xarax

Lance Lamboy said:
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.
 
G

Guest

xarax said:
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
 
C

Chris Smith

Dario said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top