• Post Reply Bookmark Topic Watch Topic
  • New Topic

interface rule inconsistency?  RSS feed

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems strange. The class that implements this interface is not public, so the interface method is not actually public, but the compiler doesn't care.



 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface methods are implicitly public, even if "public" is left off the declaration. I prefer to put it in, but it's not necessary.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I realize that. The thing is the class that implements it doesn't have to be public, which means it's not really public. A method is only accessible if its class is. On second thought this pertains to all methods and classes, or else there could be no private that included methods from java.lang
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface method is either public or not used. Not used and public are not the same thing. Not used or default limits the method to it's package whereas public can be accessible from outside of the package. Also declaring it public requires the interface to be saved in a file with the same name.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:No, I realize that. The thing is the class that implements it doesn't have to be public, which means it's not really public. A method is only accessible if its class is. On second thought this pertains to all methods and classes, or else there could be no private that included methods from java.lang


Interface methods are quite limited in their ability. Whether the class that implements it is public or not doesn't matter.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote: . . .
Interface methods are quite limited in their ability. . . .
What does that mean?

As Greg Charles says, interface methods are implicitly public.
The Java® Language Specification, in the link just given, wrote:Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Brian Barrick wrote: . . .
Interface methods are quite limited in their ability. . . .
What does that mean?

As Greg Charles says, interface methods are implicitly public.
The Java® Language Specification, in the link just given, wrote:Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.


I mean that their sole purpose is only to declare the methods a class would use. How the method does it is up to the method being declared in the class.

Yes, I was mistaken on the public/default access. That is for the interface itself not the method. However if the interface is not declared public then would the method not be also limited to classes within the same package?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote: . . . However if the interface is not declared public then would the method not be also limited to classes within the same package?
Unless it is extended by another interface or implemented by a class with public access, yes.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I am getting confused about access modifiers
This means if a class has public or protected members it must be public but compiler don't notice that..why?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, let's assume you're writing code outside the package where Abc and Xyz are declared. That code cannot instantiate an Xyz object, or even have a variable or field of type Xyz. However, let's assume for a moment that Abc is declared to be a public interface, and further assume there's some way for your code to get an instance of an Xyz class ... not by directly instantiating it of course, but there are other ways. For example, there could be a public factory method in code that does have access to the Xyz class, and that method is declared with return type Abc.

Now, can your code run the public method a()? Yes, it can. Why? Because it's a public method. Your code doesn't know about Xyz, but it knows about Abc and a().

If Xyz has other public methods, can your code run those? No. (At least, not directly. Possibly through reflection.) Why not? Because it doesn't know those other methods exist. The only way you could tell your code about the other methods would be by casting the instance to Xyz, and your code can't access that class in order to do that.

So, you can have public methods in non-public classes, and you can in fact call those methods. Does this really ever happen? It absolutely does. If you use a debugger to look at some of the things you get back from Java APIs or other libraries ... like a JDBC ResultSet, or an EJB, you're going to see weirdly-named implementation classes. These are probably non-public classes. In any case you would never instantiate them yourself. However, you can use them. You can call their public methods because you know the interface, and you don't care how exactly it's implemented. When you understand that, you're well on your way towards a deep understanding of object orientation, and the power it holds to simplify your programs.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:OK, let's assume you're writing code outside the package where Abc and Xyz are declared. That code cannot instantiate an Xyz object, or even have a variable or field of type Xyz. However, let's assume for a moment that Abc is declared to be a public interface, and further assume there's some way for your code to get an instance of an Xyz class ... not by directly instantiating it of course, but there are other ways. For example, there could be a public factory method in code that does have access to the Xyz class, and that method is declared with return type Abc.

Now, can your code run the public method a()? Yes, it can. Why? Because it's a public method. Your code doesn't know about Xyz, but it knows about Abc and a().

If Xyz has other public methods, can your code run those? No. (At least, not directly. Possibly through reflection.) Why not? Because it doesn't know those other methods exist. The only way you could tell your code about the other methods would be by casting the instance to Xyz, and your code can't access that class in order to do that.


would you care to elaborate this by giving up their respective codes..
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through the code for java.util.Arrays and find the toList method, it returns an instance of a private ArrayList static nested class.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In my opinion, I think that the JLS is too complex as it is ... there are already stuff that is handled by Java, that doesn't have much of a point.

So, yes, it is possible to declare public methods for a non-public class. So, what? I am sure that, once all the details are worked out, there are some conditions where this doesn't make sense. There are lot of features in Java, that when mixed may not make sense.

Basically, don't use it like that... Why should the compiler deal with how you are using it incorrectly?

Henry
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that maybe everyone's missing something.

It appears that the interface and method are defined with package scope.

Package scope is somewhat of a wart in that it's defined by the absence of a keyword rather than its presence, the way that public and private are.

In package scope, classes and methods are essentially public to other classes within the same package but not to the world at large. It's sort of a watered-down version of the C++ "friend" functionality.

Aside from allowing collusion between classes within a package, one of the other (ab)uses of package scope comes when you write unit tests with something like JUnit. Declaring a method with package scope allows the testing class to access nominally-private methods of the class under test.

And, just to pick nits, starting an interface name (or any class name) with a lower-case letter is not considered good practice. But that's a different matter.

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nah.I did noticed that as there are no access modifiers before class or interface
So it will be "package-private"
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:This seems strange. The class that implements this interface is not public, so the interface method is not actually public, but the compiler doesn't care.
. . .
You can always override a method with less restrictive access. You can override a package‑private method with public access, but not the other way round. As we saw earlier, however, interface methods are implicitly public.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!