This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have made a class public ,but i want that no one from outside the current package should be able to access my class.what should we do for this.This was asked in an interview.Please tell me the solution.
[Raj]: I have made a class public, but i want that no one from outside the current package should be able to access my class.
I have to say that the best solution by far is to simply not make the class public. If you don't want anyone outside the current package to access it, then there is absolutely no reason to make the class public. This question really makes no sense. I guess maybe they're trying to see if you know some particular bit of trivia, but really, there's no reason not to use access modifiers the way they were intended.
It's kind of like saying: You've painted your car red, but it should be blue. What do you do?
You could make everything inside the car blue, but you still have a red car (at least the body). Or you could nest your red car inside a blue car, so it would "appear" blue from the outside, but that doesn't make much sense either. Ultimately, the way to make your red car blue is to repaint it (that is, remove the public modifier).
If your class is public and you want your class to be invisible outside its package thats not possible but yes you can minimize control over that class by having its constructor with default access and other members also with same default access.
hope this satisfies your interviewer.. ...
Joined: Aug 05, 2005
Originally posted by marc weber: Nest?
Actually I didn't consider nesting. Like Jammy I was thinking of the constructor, but I was hoping to make the OP think a little.
Raj Kumar Bindal
Joined: Apr 15, 2006
I think Jammy reply can be considered and that is what i too have told.So,can i conclude that,there is no solution for that or whatever jammy has told is the maximum possible that can be done.
Joined: Jan 30, 2000
[Raj Kumar]: So,can i conclude that,there is no solution for that or whatever jammy has told is the maximum possible that can be done.
I would say that what Jammy suggests is probably the most extreme measure you should consider. It's possible to do more than that, but probably a bad idea. Then again, anything other than making the class non-public seems to be a bad idea. If you wanted to do more, despite all advice to the contrary, you could for example use Thread.currentThread().getStackTrace() (or new Throwable().getStackTrace()) to discover what else was in the current stack trace, and throw an exception if you think it necessary. Really, you can do all sorts of preposterous things if you want to. But again, the obvious solution was to simply make the class package-level rather than public to begin with. If you want to create a more convoluted, baroque solution, it's possible, sure. But why do it? [ November 22, 2007: Message edited by: Jim Yingst ]