Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding access modifiers

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nest?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic