File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why protected methods are not allowed in a Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why protected methods are not allowed in a Interface" Watch "why protected methods are not allowed in a Interface" New topic
Author

why protected methods are not allowed in a Interface

Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Hello every one,
I have very strange (some might think stupid) question for your guys.
I think the title it self explains a lot.

Why are protected methods not allowed in an Interface?

I have also found a bug regarding this. Its was closed in 1995 saying that protected method are no longer allowed in an interface.

It is strange because protected abstract method are allowed in an abstract class.

***************************************************************************
The second questin is
Can we declare a public class inside a interface? If so how can this class be accessed
example:
interface a {
class b {
public b() {
}
public void test() {System.out.println("Print me:");
}
public void printIt();
}

Is the above example valid if so how is class b accessiable.


Suman Mummaneni
Bangalore
India
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
You can't have two public classes in the same file, but it appears one can have a non-public class within an interface.
For more explanation, read the Java Language Specification about interfaces. Since an interface is intended to provide members for implementing classes to be used in different packages, its members are implicitly public and abstract, so you don't say "public abstract." The intention is for the methods to be used by other classes, so they ought not to be "protected."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Campbell Ritchie]: it appears one can have a non-public class within an interface.

Any class declared directly inside an interface is implicitly public (and static). Just as any method declared in and interface is implicitly public (and nonstatic), and any field declared there is implicitly public, static, and final.

[Suman Mummaneni ]: If so how can this class be accessed

It's accessed just like any static nested class. In your example:

Incidentally many of us find it confusing to talk about classes with names which violate standard naming conventions. So it would be preferable if your example used class names like A and B rather than a and b. Thanks.


"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
Thank you, Jim Yingst. Presumably you just miss out the "public".
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Thank you guys. But I still did not get the answer WHY are protected method not allowed in a interface.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
Because the methods of an interface are intended to form the public face of the implementing class. If they were "protected," they would have what is to all intents and purposes private access in implementing classes.
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
thank you Campbell, this has cleared a lot of issues for me.
Surinder Mehra
Ranch Hand

Joined: Aug 06, 2013
Posts: 32
    
    1
sorry guys, I still did not understand why we cant use protected with interface methods, we can use public while overidding methods in implementing class. Correct me if i am wrong. Is any thing wrong doing it this way
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Surinder Mehra wrote:sorry guys, I still did not understand why we cant use protected with interface methods, we can use public while overidding methods in implementing class. Correct me if i am wrong. Is any thing wrong doing it this way

I suggest you read Campbell's last post (albeit, from 6 years ago).

Ask yourself this: What would be the advantage of allowing an interface to define protected methods? And is it worth the extra complications for the compiler? Surely such a method could be defined in a skeleton class that implements the interface? Indeed it seems a much more logical place to put it, because a protected method is likely to have to do with an implementation of an interface.

Java's philosophy (at least while Sun was in charge) was generally to "keep things simple" or, as Einstein said: "as simple as possible, but no simpler".

If you can come up with a burning reason for allowing protected methods in interfaces, you might be able to convince Oracle to change it; but I'd sound out your idea here first.

Winston
Surinder Mehra
Ranch Hand

Joined: Aug 06, 2013
Posts: 32
    
    1
Ok, So keeping things simple is the only reason. I just wanted to know the reason why is it so. Declaring access modifier as public in implementing class was just one way of doing it.
Anyways, Thanks for quick reply. !!!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Surinder Mehra wrote:Ok, So keeping things simple is the only reason.

No. I didn't say that. What I said was: Why complicate things if there's no good reason for it?

And unless you can come up with one, I certainly can't.

Anyways, Thanks for quick reply. !!!

You're welcome.

Winston
satyam kaushik
Greenhorn

Joined: Jun 09, 2012
Posts: 3
So If you say to keep things simple why Sun won't restrict interface as public always.If they allow interface as "default"After all, it seems reasonable that one might want to create an interface intended to be used only within a single package, and thus it seems reasonable that one would not necessarily want the interface methods to have public visibility. However, Java forces me to declare any method in the interface as public, which is really not desirable.
satyam kaushik
Greenhorn

Joined: Jun 09, 2012
Posts: 3
Now the new question come into picture "If the interface intended to expose public only".Why default access modifier is allowed in its definition.The above discussion is still not reaches to the convincing stage and I hope we will collectively reach to a conclusion.Hope for Best
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
We cannot answer such questions unless we were there with the people who made that decision.
I presume they permitted one to create an interface to be used in one package only; that does not need public methods. You can implement the methods as public, of course.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

satyam kaushik wrote:Now the new question come into picture "If the interface intended to expose public only".Why default access modifier is allowed in its definition.

Because there's a big difference between making an interface package-private, and making its methods anything other than public.

As has previously been stated, an interface defines a public contract, because it defines a type. It therefore makes sense to allow its methods to be implemented by anyone who can see it. What you appear to want could easily be achieved by creating an abstract class; but TBH, I don't see the point.

If you don't like the way Java does things, I think it's incumbant on you to come up with a concrete example where allowing an interface to define non-public methods makes sense. Personally, I can't think of one (and I've been using Java for a long time ); but hey, if you can convince me, I'll happily put my hand up.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why protected methods are not allowed in a Interface