• 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

java library and public member functions (? c++ friends)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to limit access to a super classes' public methods?
say I have three classes all derived from a JPanel.
UserMap
|
FriendlyMap
|
UglyMap
|
JPanel
I have some functions in UglyMap, that I want FriendlyMap to use
but I don't want the UserMap to use. Is there a way I can say
that FriendlyMap can use UglyMap.clear(), but stop UserMap
from calling UglyMap.clear()?
Protected doesn't work.
I figured out a way that might work but is messy ( does
declaring 'private FriendlyMap.clear()' work?). This is
messy because UglyMap has about 300 methods in it.
if java had something like c++ friends ( an ugly hack, I
admit), it might help.
Is there another way to do this?

wl
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liz,
Welcome to Java Ranch! Please take a moment to change your display name to conform to our naming policy.
If all FriendlyMap and UglyMap are in the same package, and UserMap is in some other package, then UserMap can't access UglyMap's default-protected methods (those with no explicit protection level specified) but FriendlyMap can. Packages are the closest thing Java has to C++ "friends." All the classes in a package are friends at the default level.
 
Brett Bolen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what if UglyMap and FriendlyMap are different classes?
b�
 
Brett Bolen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually they are in different packages as well.
b
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Packages are the only tool you have that you can use to grant this kind of access control; you might want to use finer-grained packaging.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic