• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding access modifiers

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nest?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic