• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why protected methods are not allowed in a Interface

 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jim Yingst. Presumably you just miss out the "public".
 
Suman Mummaneni
Ranch Hand
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. But I still did not get the answer WHY are protected method not allowed in a interface.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Campbell, this has cleared a lot of issues for me.
 
Surinder Mehra
Ranch Hand
Posts: 41
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic