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
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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: 18670
[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: 32830
    
    4
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: 32830
    
    4
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: why protected methods are not allowed in a Interface
 
Similar Threads
combination of access & non-access modifiers for a class
interface and abstract class
Questions on for Kathy Sierr or Bert Bates
Abstract cannot be protected
static and abstract methods