2

Possible Duplicate:
Protected in Interfaces

The following code snippet shows that an interface in Java can have only one modifier which is public. No other modifiers (private and protected) are allowed within an interface neither with fields nor with any methods. Obviously among modifiers, private has no meaning to use within an interface but protected should be allowed within an interface because it can be incorporated by it's implementing class.

interface Demo
{
    private final static int a=10; //Will not be compiled.
    protected final static int b=20; //Will not be compiled.
    public final static int x=0;   //ok

    abstract public void showSum();
}

while an abstract class is allowed to have all the modifiers private, public and protected. My question is only that a protected modifier is not allowed within an interface that somewhat seems to be allowed. Why?

Community
  • 1
  • 1
Bhavesh
  • 4,607
  • 13
  • 43
  • 70
  • 2
    The sole purpose of an interface is to define a publicly-visible signature. – Dave Newton Nov 06 '11 at 13:52
  • 2
    Interface defines implementation independent API. There is no code that can utilize private fields and methods. Adding constants values into interface is bad practice. – viktor Nov 06 '11 at 14:01

3 Answers3

7

Obviously the best answer is "because that's how they defined it." I don't think I'd look too hard at the rationale behind decisions made when Java was initially defined; it was a long time ago, now, and experience gained by using the language has shown that many of those initial decisions were flawed.

In this case, an interface is intended to serve as a public protocol for communicating with an object, and as such, it was decided that all members must be public. This may not have been the best or most useful definition, but it's the one we have, and we have to live with it.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    `This may not have been the best or most useful definition` ... can you pls. explain why do you think so? – Azodious Nov 06 '11 at 14:01
  • 1
    The thing that I sometimes wish for is default-protected interfaces or interface methods -- i.e., the ability to keep the methods that implement an interface from being accessible outside of their package. In the current case, if an interface is part of some internal pattern, not intended for use outside of the package, the methods still have to be exposed as `public`. – Ernest Friedman-Hill Nov 06 '11 at 14:41
  • However Java is not installed on my machine and i'm not a java developer also since a long time now But one question. Can't it be achived by having `interface within a class` then assigning interface and class appropriate accessibility. – Azodious Nov 07 '11 at 07:16
  • Nope. As the original question points out, all methods in an interface are public. The methods implementing an interface's methods will always be accessible, even if the interface being implemented is not! – Ernest Friedman-Hill Nov 07 '11 at 12:59
7

All methods and fields in an interface should be declared in such a way so that they can be invoked from anywhere. Not only from within a subclass.

Only public modifier can make this happen.

However, it should be avoided to have a field in interface. if possible.

Azodious
  • 13,752
  • 1
  • 36
  • 71
2

An abstract class provides some implementation that subclasses inherit.

An interface simply defines an external API without providing any implementation. The whole idea behind an interface is that the implementation is left entirely to the implementing class.

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77