wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes public method in a private class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public method in a private class" Watch "public method in a private class" New topic

public method in a private class

Celina Paul

Joined: Nov 01, 2003
Posts: 16
I have a publc class say..
public class MainClass{
class PrivateClass{
in a file called MainClass.java
In my PrivateClass, I have public methods.
Who (all) can access these public methods ?
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

well.... actually your "class PrivateClass" isn't really private. It's of the default protection level (sometimes called "package").
The default modifier gives access to all classes in the same package.
So, your MainClass has access to PrivateClass. And also, any other class you add to this package, will have access to PrivateClass.
Does that help?
Can you write some code for a class that doesn't have access to PrivateClass?

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Celina Paul

Joined: Nov 01, 2003
Posts: 16
Hi Jess,
Yes...that is what I was thinking...
I guess my question is -
Is there anyway to access public methods of a class which has been declared private ?
It's not something that I want to do, it's just something that I want to know.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Is there anyway to access public methods of a class which has been declared private ?
Yes. Well, obviously you can access those methods from within the same class. But I'm guessing you mean, is there a way to access the public methods from outside the private class - and the answer is yes. However this requires that the private class extend a public class, or implement a public interface, which declares the methods in question.

Here class C is private, which means that D can't call it directly. But B can, and B is allowed to return a reference to C, and pass it to D. D does not know much about what B really is, but D does know that B is an A, and has a foo() method, so D is allowed to call that method.
This technique may seem a bit convoluted or arbitrary here, as the classes invovled don't have any apparent purpose. But this technique is used reasonably often in actual programming, to provide an implementation of a public interface without expsong details about the particular implementation used.
For simplicity I've just discussed public vs. private here. We could also apply the same discussion to public vs. protected, private vs. package, package vs. public, package vs. protected, or protected vs. public - the basic idea is the same for any method with higher access level than the class which contains it. Of course if we're implementing an interface, the interface methods are necessarily public - but protected and package are relevant if we're extending a class rather than implementing an interface.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: public method in a private class
Similar Threads
How do I do a partial implemantation of an Interface
Declaring members of a non-public class as public
passing data between 2 classes
methods will execute?
Design questions (with RMI)