Access specifiers for interface methods

When we implement the interface ,we have to specify the implementing method access specifier to be "PUBLIC" ,but not "PROTECTED" or "PRIVATE".

Compiler is giving an error -- attempting to assign weaker access privileges ,when we specify protected.

what is the internal reason for this?Why we shouldnt make the access weaker in the implementing class.

Can any one help me on this.Your help is highly appreciated.

[436 byte] By [suman] at [2007-9-30 19:26:19]
# 1
An interface is meant to guarantee that an implementing class can provide that functionality. By changing it so say private when it says public you can no longer guarantee that the method will be available to class that tries to use that interface.
bjon045 at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 2

To clarify:

Consider you have 2 classes both implementing the same interface. Now if the interface defines a method doThis() to be public and the first implemented it with a public method and the second privately (incorrect).

if ((Impl1 instanceOf Interface) && (Impl2 instanceOf Interface)) {

((Interface)Impl1).doThis()

((Interface)Impl2).doThis() //would fail as private methods are not accessible

}

bjon045 at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 3
Can u explain it in more detail.
suman at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 4
While ,as an interface,it describes the common behavior of its derived classed,and by default ,fields and methods is Public.In java ,you cann't mark a method in subclass less accessible than baseclass.
Mars_cl at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 5

> When we implement the interface ,we have to specify

> the implementing method access specifier to be

> "PUBLIC" ,but not "PROTECTED" or "PRIVATE".

>

> Compiler is giving an error -- attempting to assign

> weaker access privileges ,when we specify protected.

>

>

> what is the internal reason for this?

There is absolutely no point in having a private interface method. The interface represents a visible abstraction and private methods are never visible so it is a contradiction in terms.

An interface is intended to represent an abstraction that a user (software) uses. Protected via child/parent represents a usage that is restricted to a child from a parent. The child can already see the parent so there is no point in having an abstraction for the child. And it would probably be unwise to limit a child by such an abstraction.

Protected via the package and interfaces is more contentious as to why it is not allowed. There are those that argue that this should be allowed so that a package can use interfaces but restrict them to the package. To me this seems like a minor point given that most interfaces will probably represent an abstraction to other packages and not within a single package. This applies specifically to default access as well.

jschell at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 6

Let me be more specific

interface MyInterface

{

public void display();//here the access specifier is public

}

class Myclass implements MyInterface

{

void display(){} //this wont compile.,because the access(default) is weaker than public

}

Why the designers didnt allow to make the access of the implementing class weaker.

suman at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 7

>

> Why the designers didnt allow to make the access of

> the implementing class weaker.

That has to do with method inheritance not interfaces.

However the reason is similar.

A class defines its visibility. A derived class can not change that visibility. Allowing that would break the contract of the parent class (or interface.)

jschell at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 8
It does seem like quite a puzzle: if you can declare a non-public interface then why can't you declare non-public interface members?
java@stevencoco.com at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...
# 9

> It does seem like quite a puzzle: if you can declare a

> non-public interface then why can't you declare

> non-public interface members?

Private and protected are only valid when the interface is nested in a class.

Default access is the exceptional one. And I am probably not the one to justify that.

jschell at 2007-7-6 23:39:48 > top of Java-index,Administration Tools,Sun Connection...