wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces with default access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces with default access" Watch "Interfaces with default access" New topic
Author

Interfaces with default access

Trey Carroll
Greenhorn

Joined: May 06, 2008
Posts: 17
Hello all,

I'm studying K&B and I just came across something that I thought might benefit the community.

K&B 1.5 pg 69 says
"Interfaces can be implemented by any class, from any inheritance tree".
That sounded fishy to me so I coded up a little sample app to test it.





The statement in K&B is very broad and seems to imply that any interface in any package will always be accessible to any other class. I started thinking that all interfaces must be public implicitly or something, but I have verified that this is not true. If you remove the public access modifier from the Testable interface in the code above, the Test class will no longer compile. You get this error:
pkg1.Testable is not public in pkg1; cannot be accessed from outside package
import pkg1.Testable;


The moral of the story is that the K&B statement is misleading. Interfaces can have default access; if they do you may not implement them from outside their own package.

Hope this is beneficial to someone else.


Trey Carroll<br />======================<br />Studying for the SCJP
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Interfaces can be implemented by any class, from any inheritance tree

I guess it implies that any class can implement the interface even if the inheritance trees are different. But for that still the interface should be visible or accessible. You can't access a interface in other package if it has default access modifier in some other package. Interfaces are implicitly abstract but not implicitly public. In K&B itself on page 21 you can find this comment too.

The public modifier is required if you want the interface to have public rather than default access.
.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Originally posted by Trey Carroll:
The moral of the story is that the K&B statement is misleading.
Not really. You misinterpreted it. This statement implies nothing about access modifiers. Rather it is pointing out that unlike base classes, interfaces do not need to be part of the inheritance hierarchy in order to be implemented.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Trey Carroll
Greenhorn

Joined: May 06, 2008
Posts: 17
Now I understand what was meant by the statement.

Thank you for taking the time to clarify it for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces with default access
 
Similar Threads
Modifier protected does not hide inherited members to (static) code of superclass package Query
Private Constructors
Declaring Broader Checked Exceptions when implementing an interface
JAVA Package Query
Modifier protected does not hide inherited members to (static) code of superclass package