aspose file tools*
The moose likes Beginning Java and the fly likes Regarding access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding access modifiers" Watch "Regarding access modifiers" New topic
Author

Regarding access modifiers

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3568
    
  15
If you can't change the access specifier of the class, then you need to change the access specifier of part of the class. What do all classes have that might be useful in this situation ?


Joanne
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Nest?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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).
Jammy Wells
Greenhorn

Joined: Oct 04, 2007
Posts: 17
Raj

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.. ...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3568
    
  15
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
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding access modifiers