• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

??????? Friend class in Java ???????  RSS feed

Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I have one questions which a Comparison question between C++ and Java. In C++ there is concept of friend class due to which one can access private members of that class. As there is no friend keyword in java, how can we achieve the same functionality in java?

Amit Kulkarni
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this, and reply with the result


I'll come back later and check.

/Svend Rost

edit: me no type gud today
[ June 28, 2005: Message edited by: Svend Rost ]
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you ask yourself "How can we achieve this functionality?" you should ask "Why do we want to achieve this functionality?"

I am an Object. I have an instance variable called spine. My spine is private; only I can access it (I don't even have getSpine() or setSpine() methods...). If I need adjustments to my spine, I have two options: I can find either an instance of a Chiropracter object or an instance of a Surgeon object. The Chiropracter object can only access my spine through the proper interface methods (pushOnBackHere() and pushOnBackThere()), while a Surgeon can open me up and access the spine directly. But since I don't have any real control over what that Surgeon does (Objects cannot sue for malpractice) and since I don't know what else the Surgeon might be doing to me (I'm under general anesthesia), I had better make sure that Surgeon is a friend. Now, in Java, there is no friend keyword, so two things must be true: my spine must not be private (it must be default or protected), and the Surgeon must be in the same package as myself (or be a subclass of me, for things that are protected.)

Clearly it is much safer for me to go the Chiropracter as I have more control over what can happen to me. If my spine is private, then in Java no Surgeon can touch the spine directly. I would have to have specific methods to allow the Surgeon in and limit exactly what the Surgeon can do; maybe some "passive" method like

This way, the Surgeon can only access my spine when I allow it. (Ok, the Surgeon could keep a reference to my spine, but the analogy breaks down there. I mean, trying to picture that is just weird....)

Anyway, what I'm saying is that in actuality you very rarely need the "friendly" access; there's much safer ways of doing things nowadays.
[ June 28, 2005: Message edited by: Joel McNary ]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!